miriup.de

...because open source matters

  • Increase font size
  • Default font size
  • Decrease font size

Python singleton pattern

E-mail Print

After a quick search where I didn't find a singleton implementation for new-style classes in Python. For classic classes there's the excellent Borg pattern. So here's singleton for new-style classes:

class Singleton(object):

__instance = None

@classmethod
def getInstance(cl):
"""
Get the global engine instance or create one if none exists yet.
"""
if cl.__instance is None:
cl.__instance = cl()

return cl.__instance
Attachments:
Download this file (Singleton.py.txt)Singleton.py[ ]0 Kb17/09/10 02:30
Last Updated on Friday, 17 September 2010 02:22  

Add comment


Security code
Refresh