Created
January 30, 2010 17:10
-
-
Save ishikawa/290628 to your computer and use it in GitHub Desktop.
This file contains 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
""" | |
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