Skip to content

Instantly share code, notes, and snippets.

@mrts
Created January 30, 2011 18:58
Show Gist options
  • Save mrts/803121 to your computer and use it in GitHub Desktop.
Save mrts/803121 to your computer and use it in GitHub Desktop.
Example of chaining descriptors and sharing a cache between them.
"""
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