Skip to content

Instantly share code, notes, and snippets.

@cameronp98
Created January 9, 2014 22:04
Show Gist options
  • Select an option

  • Save cameronp98/8342927 to your computer and use it in GitHub Desktop.

Select an option

Save cameronp98/8342927 to your computer and use it in GitHub Desktop.
Singleton-esque one instance factory(?) in Python
class Factory(object):
inst=None
@classmethod
def get(self):
if self.inst == None:
# new instance of self
self.inst = self # instantiate() to run __init__
self.inst.__init__(self)
# apply @classmethod to all methods
for name,attr in self.__dict__.items():
if callable(getattr(self, name)) and name != "__init__":
setattr(self, name, classmethod(attr))
return self.inst
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment