sharing objects between classes

Gerardo Herzig gherzig at fmed.uba.ar
Mon Jan 28 10:26:32 EST 2008


Hi all. Im wondering the way to share a database connection between some 
classes:

So far, i came up with a simple class schema, where each class means 
each different relation, i mean i have the follow classes

class Database(object):
  ## make the connection
  self.conn = make_conn(....)

class Table(object):
  def get_fields:
    ....

And at this point i dont know how to use the Database.conn attribute, 
since the get_fields method will perform a query over the given database.
At first, i just define the Table class as a inner class of Database, 
but if i try a   
class Database(object):
  ## make the connection
  def __init__(self):
    self.conn = sql_connect(....)
    self.table = Table('foo')

  class Table(object): ## inner class
    def get_fields(self, name):
    ....

I get a "NameError: global name 'Table' is not defined".

So, which would the right pattern to use here? Using a global module? I 
dont know why, but i dont like that idea too much.

Any comments will be appreciated!
Thanks!

Gerardo




More information about the Python-list mailing list