title: Access parent object from submodule
body: <p>I'm writing a module for accessing some data from a local SQLite Database, and would like the end-user functionality to work like this:</p>
  
  <pre><code>import pyMyDB
  myDBobj = pyMyDB.MyDB( '/path/to/DB/file' )  # Instance of the class MyDB
  </code></pre>
  
  <p>User has simply created an object that connects to a database.<br>
  Then, depending on the need, I want to have different submodules the user can access to work on the data from the database.  For example, <code>Driving</code> provides certain functions while <code>Walking</code> provides others, which I'd love to be accessed like so:</p>
  
  <pre><code>result = myDBobj.Driving.GetTachometerReading()
  </code></pre>
  
  <p>or</p>
  
  <pre><code>result = myDBobj.Walking.GetShoeType()
  </code></pre>
  
  <p><strong>My question is</strong> about how to make the submodules <code>Driving</code> &amp; <code>Walking</code> have access to the parent <em>Object</em> (not just the Parent module's class/functions, but the instantiated object).</p>
  
  <p>If I make my file heirarchy something like this:</p>
  
  <pre><code>pyMyDB/
      __init__.py      # imports the below modules as so:
      MyDB_class.py    # imported as *, contains MyDB class definition
      Driving.py       # imported normally via `import Driving`
      Walking.py       # imported normally
  </code></pre>
  
  <p>where <code>__init__.py</code> imports <code>MyDB_class.py</code> (which contains the Database class) and also imports <code>Driving/Walking.py</code>, then
  the user can do <code>pyMyDB.Driving.some_func()</code>, but <code>some_func()</code> won't actually have access to an instantiated <code>MyDB</code> object, right?</p>
  
  <p><strong>Has anyone come across a way to have the sub-modules access the instantiated object?</strong></p>
  
  <hr>
  
  <p>Here is my current solution.  It seems overly complicated.</p>
  
  <p>First, I have to use an additional <code>globals.py</code> file to overcome circular module imports (<a href="https://stackoverflow.com/questions/13034496/using-global-variables-between-files-in-python%20This%20Hint">Like this hint</a>).</p>
  
  <p>In the <code>Driving</code> module I make a new class called <code>Drv</code>, which is used to make a duplicate of the <code>MyDBobj</code>, but also has the extra methods I want, such as <code>Driving.GetTachometerReading()</code>.</p>
  
  <p><em>Driving.py:</em></p>
  
  <pre><code>class DrvClass( MyDB ):
      def __init__(self, MyDBobj):
          self.attribute1 = MyDBobj.attr1
          self.attribute2 = MyDBobj.attr2
          ....   # copy all attribute values!
  
      def GetTachometerReading(self):
          ....   #some function here
  </code></pre>
  
  <p>Then, to accomplish the sub-indexing (<code>MyDBobj.Driving.GetTach()</code>), from within the <code>Driving.py</code> module, I add a <code>Driving()</code> method to the <code>MyDB</code> class via</p>
  
  <pre><code>def temp_driving(self):
      return DrvClass( self )    # pass the MyDBobj
  
  MyDB.Driving = temp_driving    # add the above method to the MyDB class
  </code></pre>
  
  <p>So now a user can do: <code>MyDBobj.Driving().GetTachometerReading()</code>, where <code>MyDBobj.Driving()</code> returned the new <code>DrvClass</code> object that has the new <code>GetTachometer()</code> function.  I don't like the fact that I must call <code>Driving()</code> as a function.</p>
  
  <p>What do you think - is there a better/simpler way?</p>
  
  <p>Btw the reason I want the MyDB class to be separate is because our access methods may change, without changing the analysis functions (Driving/Walking), or vice-versa.  Thus I don't want to just add the MyDB access techniques directly to separate Driving &amp; Walking modules.</p>
  
  <p>Thanks in advance for your sage advice!</p>