an example of a singleton design pattern in python?

D

Daniel Ortmann

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: (e-mail address removed) / 507.535.3861 / 63861 int / 8012.3861 gdds
home: (e-mail address removed) / 507.288.7732, 2414 30Av NW #D, Rochester MN 55901
gpg/pgp public key: http://wwwkeys.us.pgp.net
jabber: (e-mail address removed) / (e-mail address removed)
 
V

Ville Vainio

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
 
P

Peter Hansen

Ville said:
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
 
V

Ville Vainio

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.
 
A

Aahz

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.
 
J

Jeff Epler

* 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:.... x = []
.... def f(self): return self.x
.... def g(self, arg): self.x.append(arg)
....
[3]
 
A

Aahz

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top