an example of a singleton design pattern in python?

Discussion in 'Python' started by Daniel Ortmann, Jan 20, 2004.

  1. Hello,

    Does anyone have an example of a singleton design pattern in python?

    Thanks!

    --
    Daniel Ortmann, LSI Logic, 3425 40th Av NW, Suite 200, Rochester MN 55901
    work: / 507.535.3861 / 63861 int / 8012.3861 gdds
    home: / 507.288.7732, 2414 30Av NW #D, Rochester MN 55901
    gpg/pgp public key: http://wwwkeys.us.pgp.net
    jabber: /
    Daniel Ortmann, Jan 20, 2004
    #1
    1. Advertising

  2. Daniel Ortmann

    Rene Pijlman Guest

    Rene Pijlman, Jan 20, 2004
    #2
    1. Advertising

  3. Lawrence Oluyede, Jan 20, 2004
    #3
  4. Daniel Ortmann

    Yermat Guest

    Yermat, Jan 20, 2004
    #4
  5. Daniel Ortmann

    Ville Vainio Guest

    >>>>> "Daniel" == Daniel Ortmann <> writes:

    Daniel> Hello, Does anyone have an example of a singleton design
    Daniel> pattern in python?


    This trivial snippet might be enough for your needs:

    class Foo():
    pass

    _inst = None

    def fooinstance():
    if not _inst:
    _inst = Foo()
    return _inst

    --
    Ville Vainio http://tinyurl.com/2prnb
    Ville Vainio, Jan 20, 2004
    #5
  6. Daniel Ortmann

    John Roth Guest

    John Roth, Jan 20, 2004
    #6
  7. Daniel Ortmann

    Peter Hansen Guest

    Ville Vainio wrote:
    >
    > >>>>> "Daniel" == Daniel Ortmann <> writes:

    >
    > Daniel> Hello, Does anyone have an example of a singleton design
    > Daniel> pattern in python?
    >
    > This trivial snippet might be enough for your needs:
    >
    > class Foo():
    > pass
    >
    > _inst = None
    >
    > def fooinstance():
    > if not _inst:
    > _inst = Foo()
    > return _inst


    Looks like at least two errors, one being the parentheses on the
    class name, and the other being a failure to flag _inst as being
    global...

    -Peter
    Peter Hansen, Jan 20, 2004
    #7
  8. Daniel Ortmann

    Ville Vainio Guest


    >> This trivial snippet might be enough for your needs:
    >>
    >> class Foo(): pass
    >>
    >> _inst = None
    >>
    >> def fooinstance(): if not _inst: _inst = Foo() return _inst


    peter> Looks like at least two errors, one being the parentheses
    peter> on the class name, and the other being a failure to flag
    peter> _inst as being global...

    True. I guess the idea got through anyway. It was late :p.

    --
    Ville Vainio http://tinyurl.com/2prnb
    Ville Vainio, Jan 21, 2004
    #8
  9. Daniel Ortmann

    Aahz Guest

    In article <>,
    Daniel Ortmann <> wrote:
    >
    >Does anyone have an example of a singleton design pattern in python?


    It appears that none of the other responses mentioned the two simplest
    techniques:

    * Within a single module, use a global class as your singleton object.

    * Across modules, use a module as your singleton object.
    --
    Aahz () <*> http://www.pythoncraft.com/

    A: No.
    Q: Is top-posting okay?
    Aahz, Jan 22, 2004
    #9
  10. Daniel Ortmann

    Jeff Epler Guest

    On Wed, Jan 21, 2004 at 07:38:26PM -0500, Aahz wrote:
    > * Within a single module, use a global class as your singleton object.


    This is a little bit inconvenient, because you must either mark all
    methods as staticmethod (and refer to the class by name), or you must
    mark all methods as classmethod.

    This metaclass can help. It makes all functions into staticmethods at
    the time the class is created:

    from types import FunctionType

    class SingletonClassMeta(type):
    def __new__(cls, name, bases, dict):
    for k, v in dict.iteritems():
    if isinstance(v, FunctionType):
    dict[k] = classmethod(v)
    return (super(SingletonClassMeta, cls).
    __new__(cls, name, bases, dict))

    class Singleton(object):
    """All methods in a Singleton object are made class methods"""
    __metaclass__ = SingletonClassMeta

    Probably __init__ should be made to raise an exception (RuntimeError,
    "Singletons cannot be instantiated"))...

    An example:
    >>> class C(Singleton):

    .... x = []
    .... def f(self): return self.x
    .... def g(self, arg): self.x.append(arg)
    ....
    >>> C.f()

    []
    >>> C.g(3)
    >>> C.f()

    [3]
    Jeff Epler, Jan 22, 2004
    #10
  11. Daniel Ortmann

    Aahz Guest

    In article <>,
    Jeff Epler <> wrote:
    >On Wed, Jan 21, 2004 at 07:38:26PM -0500, Aahz wrote:
    >>
    >> * Within a single module, use a global class as your singleton object.

    >
    >This is a little bit inconvenient, because you must either mark all
    >methods as staticmethod (and refer to the class by name), or you must
    >mark all methods as classmethod.


    That's assuming one needs methods, of course; many times, the singleton
    is just used for storing attributes.
    --
    Aahz () <*> http://www.pythoncraft.com/

    A: No.
    Q: Is top-posting okay?
    Aahz, Jan 22, 2004
    #11
    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. =?Utf-8?B?RGlmZmlkZW50?=

    Singleton design pattern

    =?Utf-8?B?RGlmZmlkZW50?=, Jan 11, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    424
    Scott Allen
    Jan 11, 2005
  2. Pallav singh
    Replies:
    0
    Views:
    354
    Pallav singh
    Jan 22, 2012
  3. Pallav singh
    Replies:
    0
    Views:
    399
    Pallav singh
    Jan 22, 2012
  4. Pallav singh
    Replies:
    1
    Views:
    449
    Peter Remmers
    Jan 22, 2012
  5. Wilhelm
    Replies:
    1
    Views:
    164
Loading...

Share This Page