Created
January 30, 2011 18:58
-
-
Save mrts/803121 to your computer and use it in GitHub Desktop.
Example of chaining descriptors and sharing a cache between them.
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
""" | |
Example of chaining descriptors and sharing a cache between them. | |
""" | |
class LeafDescriptor(object): | |
def __get__(self, obj, cls): | |
return obj.parent._cache | |
class Bar(object): | |
baz = LeafDescriptor() | |
def __init__(self, parent): | |
self.parent = parent | |
class BarDescriptor(object): | |
""" | |
BarDescriptor returns a Bar object. | |
""" | |
def __get__(self, obj, cls): | |
return Bar(obj) | |
class Foo(object): | |
_cache = {'empty': None} | |
bar = BarDescriptor() | |
if __name__ == '__main__': | |
print Foo().bar.baz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment