sharing objects between classes

Discussion in 'Python' started by Gerardo Herzig, Jan 28, 2008.

  1. 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
    Gerardo Herzig, Jan 28, 2008
    #1
    1. Advertising

  2. Gerardo Herzig wrote:

    > 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!


    Take a look at the sources of e.g. SQLObject and how they do it (in SO, the
    concept is called "HUB")

    Essentially, you set a reference to a global connection object that itself
    isn't a simple global but useses threading.local to have one connection per
    thread. Then you can access that connection transparently.

    Diez
    Diez B. Roggisch, Jan 28, 2008
    #2
    1. Advertising

  3. Diez B. Roggisch a écrit :
    > Gerardo Herzig wrote:
    >
    >> 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:
    >> ....
    >>

    (snip)
    >
    > Take a look at the sources of e.g. SQLObject and how they do it (in SO, the
    > concept is called "HUB")
    >

    And while you're at it, take a look at SQLAlchemy too, and ask yourself
    if you really need to roll your own solution !-)
    Bruno Desthuilliers, Jan 28, 2008
    #3
  4. Gerardo Herzig

    Guest

    > Diez B. Roggisch a écrit :
    >> Gerardo Herzig wrote:
    >>
    >>> 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:
    >>> ....
    >>>

    > (snip)
    >>
    >> Take a look at the sources of e.g. SQLObject and how they do it (in SO,
    >> the
    >> concept is called "HUB")
    >>

    > And while you're at it, take a look at SQLAlchemy too, and ask yourself
    > if you really need to roll your own solution !-)


    Yes, i dont need to reinvent the wheel, i know, it just seems like a
    pattern i will have to deal with, not just in this case. SQLObject seems
    like a big peace of code to read. At least to me (not that good
    programmer).

    I will take a look at SQLAlchemy, and see a little more.
    Thanks!

    Gerardo
    , Jan 28, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    401
  2. Nadav
    Replies:
    0
    Views:
    267
    Nadav
    Jun 16, 2004
  3. =?Utf-8?B?QWxleCBNYWdoZW4=?=

    Sharing Objects Between ASP.NET "Sites"/Projects

    =?Utf-8?B?QWxleCBNYWdoZW4=?=, Jun 3, 2007, in forum: ASP .Net
    Replies:
    5
    Views:
    863
    Steven Cheng[MSFT]
    Jun 5, 2007
  4. Leon Bogaert
    Replies:
    6
    Views:
    136
    ara.t.howard
    May 11, 2008
  5. Trevoke
    Replies:
    10
    Views:
    192
    Brian Candler
    Jul 22, 2009
Loading...

Share This Page