Skip to content

Instantly share code, notes, and snippets.

@ishikawa
Created January 30, 2010 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ishikawa/290628 to your computer and use it in GitHub Desktop.
Save ishikawa/290628 to your computer and use it in GitHub Desktop.
"""
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