Created
January 9, 2014 22:04
-
-
Save cameronp98/8342927 to your computer and use it in GitHub Desktop.
Singleton-esque one instance factory(?) in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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