Keeping a database connection with a Singleton?

Discussion in 'Python' started by exhuma.twn, Sep 19, 2007.

  1. exhuma.twn

    exhuma.twn Guest

    I remember reading about the Singleton pattern in python and how it's
    an unpythonic pattern and all. At the time I did not need the
    Singleton anyways, so I just glanced over the document.

    But, setting this aside: I have an application where I have a
    connection to a database. At some point in the application I open up a
    new window. The new windows resides in a different module. So I have a
    directory structure like this:

    - mainapp.py
    - newwindow.py

    So I import "newwindow" in "mainapp" so I can instantiate and display
    it. Meanwhile, the main-app has an open connection to the database.
    What's the cleanest way to hand this connection to the new window? I
    can see several possibilities:

    1) Simply pass the connection as paramtere to the constructor of new-
    window.
    2) Use the "Singleton" deisign pattern to keep a reference to the
    connection
    3) Open up a completely new connection to the database in the new
    window.

    Now, option 1) is clearly the easiest to implement, however, I somehow
    tend to use option 2 (the singleton) as it's more flexible. Option 3
    looks ugly to me.

    This is a stuation I run into many times. And I am always faced with
    the same choice. And I never know which one to chose. And now that I
    am getting more and more comfortable with the basics of python, I
    would like to know if I am missing something more "pythonic".

    So, what would you recommend?
     
    exhuma.twn, Sep 19, 2007
    #1
    1. Advertising

  2. exhuma.twn

    Jason Guest

    On Sep 19, 7:26 am, "exhuma.twn" <> wrote:
    > I remember reading about the Singleton pattern in python and how it's
    > an unpythonic pattern and all. At the time I did not need the
    > Singleton anyways, so I just glanced over the document.
    >
    > But, setting this aside: I have an application where I have a
    > connection to a database. At some point in the application I open up a
    > new window. The new windows resides in a different module. So I have a
    > directory structure like this:
    >
    > - mainapp.py
    > - newwindow.py
    >
    > So I import "newwindow" in "mainapp" so I can instantiate and display
    > it. Meanwhile, the main-app has an open connection to the database.
    > What's the cleanest way to hand this connection to the new window? I
    > can see several possibilities:
    >
    > 1) Simply pass the connection as paramtere to the constructor of new-
    > window.
    > 2) Use the "Singleton" deisign pattern to keep a reference to the
    > connection
    > 3) Open up a completely new connection to the database in the new
    > window.
    >
    > Now, option 1) is clearly the easiest to implement, however, I somehow
    > tend to use option 2 (the singleton) as it's more flexible. Option 3
    > looks ugly to me.
    >
    > This is a stuation I run into many times. And I am always faced with
    > the same choice. And I never know which one to chose. And now that I
    > am getting more and more comfortable with the basics of python, I
    > would like to know if I am missing something more "pythonic".
    >
    > So, what would you recommend?


    You don't need a way to make every instance the same. You need a way
    where every instance shares the same state. Aka, the Borg pattern,
    courtesy of Alex Martelli. It's in the Python Cookbook at "http://
    aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531". Good luck!

    --Jason
     
    Jason, Sep 19, 2007
    #2
    1. Advertising

  3. exhuma.twn

    Peter Otten Guest

    exhuma.twn wrote:

    > I remember reading about the Singleton pattern in python and how it's
    > an unpythonic pattern and all. At the time I did not need the
    > Singleton anyways, so I just glanced over the document.
    >
    > But, setting this aside: I have an application where I have a
    > connection to a database. At some point in the application I open up a
    > new window. The new windows resides in a different module. So I have a
    > directory structure like this:
    >
    > - mainapp.py
    > - newwindow.py
    >
    > So I import "newwindow" in "mainapp" so I can instantiate and display
    > it. Meanwhile, the main-app has an open connection to the database.
    > What's the cleanest way to hand this connection to the new window? I
    > can see several possibilities:
    >
    > 1) Simply pass the connection as paramtere to the constructor of new-
    > window.
    > 2) Use the "Singleton" deisign pattern to keep a reference to the
    > connection
    > 3) Open up a completely new connection to the database in the new
    > window.
    >
    > Now, option 1) is clearly the easiest to implement, however, I somehow
    > tend to use option 2 (the singleton) as it's more flexible. Option 3
    > looks ugly to me.
    >
    > This is a stuation I run into many times. And I am always faced with
    > the same choice. And I never know which one to chose. And now that I
    > am getting more and more comfortable with the basics of python, I
    > would like to know if I am missing something more "pythonic".
    >
    > So, what would you recommend?


    Passing the connection as a parameter is the best approach because it
    keeps the dependencies explicit. Also, it allows you to switch between
    singleton and one connection per window without ever touching the newwindow
    module.

    By the way, there is a pythonic (near) singleton: the module. So if you go
    with option 2, just move the connection setup into a separate module that
    you can import into client code.

    Peter
     
    Peter Otten, Sep 19, 2007
    #3
  4. exhuma.twn

    exhuma.twn Guest

    On Sep 19, 3:45 pm, Peter Otten <> wrote:
    > exhuma.twn wrote:

    [...]
    >
    > By the way, there is a pythonic (near) singleton: the module. So if you go
    > with option 2, just move the connection setup into a separate module that
    > you can import into client code.
    >
    > Peter


    You say "(near)" singleton. What's the difference then?
     
    exhuma.twn, Sep 19, 2007
    #4
  5. exhuma.twn

    Peter Otten Guest

    exhuma.twn wrote:

    > On Sep 19, 3:45 pm, Peter Otten <> wrote:
    >> exhuma.twn wrote:

    > [...]
    >>
    >> By the way, there is a pythonic (near) singleton: the module. So if you go
    >> with option 2, just move the connection setup into a separate module that
    >> you can import into client code.
    >>
    >> Peter

    >
    > You say "(near)" singleton. What's the difference then?


    I was thinking of the main script of an application. If you import
    that by its name

    import main # assuming the file is main.py

    you end up with two instances sys.modules["main"] and sys.modules["__main__"].

    Peter
     
    Peter Otten, Sep 19, 2007
    #5
  6. exhuma.twn

    exhuma.twn Guest

    On Sep 19, 4:03 pm, Peter Otten <> wrote:
    > exhuma.twn wrote:
    > > On Sep 19, 3:45 pm, Peter Otten <> wrote:
    > >> exhuma.twn wrote:

    > > [...]

    >
    > >> By the way, there is a pythonic (near) singleton: the module. So if you go
    > >> with option 2, just move the connection setup into a separate module that
    > >> you can import into client code.

    >
    > >> Peter

    >
    > > You say "(near)" singleton. What's the difference then?

    >
    > I was thinking of the main script of an application. If you import
    > that by its name
    >
    > import main # assuming the file is main.py
    >
    > you end up with two instances sys.modules["main"] and sys.modules["__main__"].
    >
    > Peter


    I see. Thanks. I will give it a go.
     
    exhuma.twn, Sep 19, 2007
    #6
    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. Proton Projects - Moin

    Singleton - Whether Cloneable overrides Singleton

    Proton Projects - Moin, Mar 26, 2007, in forum: Java
    Replies:
    4
    Views:
    3,335
    Proton Projects - Moin
    Mar 27, 2007
  2. Stef Mientki
    Replies:
    4
    Views:
    967
    Dennis Lee Bieber
    Sep 9, 2010
  3. Wilhelm
    Replies:
    1
    Views:
    185
  4. Trans
    Replies:
    12
    Views:
    304
    Robert Klemme
    Sep 14, 2007
  5. Paul McMahon
    Replies:
    3
    Views:
    222
    David A. Black
    Jun 9, 2008
Loading...

Share This Page