Transition from small scripts to bigger apps is not easy
<p>I believe that readability and the KISS principle are the most important things in programming. That's why I use Python :)<br>
And here is exact situation, which I encounter very often:</p>

<p>Say, I have a nice and clean script, which is a wrapper for database handling:</p>

<pre><code>import database_schema as schema
loader = schema.Loader("sqlite:///var/database.db")
session = loader.session

def addUser(name, full_name, password):
    user = schema.User(name, full_name, password)
    session.add(user)
    session.commit()

def listUsers():
    all_users = session.query(schema.User).all()
    return all_users
</code></pre>

<p>Which is used like this:</p>

<pre><code>import database
database.addUser("mike", "Mike Driscoll", "password")
database.listUsers()
</code></pre>

<p>At some point, I want to rewrite that module, so that it can work with databases on different path (for unit testing, for instance).</p>

<p>So, what are my options?</p>

<ol>
<li><p>The most intuitive thing is to add <code>database_path == ""</code> variable, and then... what? Setting it with <code>setPath(new_path)</code> function, and then adding exception (<code>if database_path == "": raise SomeException</code>) to every single function, is just ugly and shouldn't be done by anyone.</p></li>
<li><p>Full featured class, with setting the <code>self._database_path</code> at initialization time.</p></li>
</ol>

<p>Which is then used this way:</p>

<pre><code>from database import Database
database = Database("sqlite:///var/database.db")
database.addUser("mike", "Mike Driscoll", "password")
database.listUsers()
</code></pre>

<p>This is already more lines of code than in the first example, and addition of the naming problem: having a class called <code>Database</code> in the module <code>database</code> is kind of dumb, no?</p>

<p>Sorry for the long read, here my final questions:</p>

<ol>
<li>Why there's no such thing as <code>__init__</code> functions for modules, in Python?</li>
<li>Am I missing something (static class variables, etc), that can do what I want (a constant set at the import time) the easy and clean way, that is still not far away from a module with a bunch of simple functions that was at first?</li>
</ol>

<p>P.S. sorry for my English.</p>

<p><strong>Edit:</strong></p>

<p>So, to make this clear, how this code may look like in my imaginative Python universe:</p>

<pre><code>import database_schema as schema

def __init__(database_path):
    loader = schema.Loader(database_path)
    global session
    session = loader.session

def addUser(name, full_name, password):
    user = schema.User(name, full_name, password)
    session.add(user)
    session.commit()

def listUsers():
    all_users = session.query(schema.User).all()
    return all_users
</code></pre>

<p>And used like this:</p>

<pre><code>import database("sqlite:///var/database.db")

database.addUser("mike", "Mike Driscoll", "password")
database.listUsers()
</code></pre>
