Skip to content

Instantly share code, notes, and snippets.

@ishikawa

ishikawa/instance.py

Created Jan 30, 2010
Embed
What would you like to do?
"""
The shared instance implementation by using
captured lexical variable bindings.
"""
class A(object):
@staticmethod
def shared():
"""
Returns the shared instance of A.
>>> A.shared() #doctest: +ELLIPSIS
<__main__.A ...>
>>> A.shared() is A.shared()
True
"""
instance = A()
A.shared = staticmethod(lambda: instance)
return instance
if __name__ == "__main__":
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.