Skip to content

Instantly share code, notes, and snippets.

@hryniuk
Created February 8, 2017 14:05
Show Gist options
  • Save hryniuk/63ca8429ca9af2595f6a3fb3f4d9b7aa to your computer and use it in GitHub Desktop.
Save hryniuk/63ca8429ca9af2595f6a3fb3f4d9b7aa to your computer and use it in GitHub Desktop.
Python descriptor example
class LogCountDescriptor:
def __init__(self, log_count=0):
self.log_count = log_count
def __get__(self, obj, type=None):
print("log from {}".format(obj))
self.log_count += 1
return self.log_count
def __set__(self, obj, value):
raise AttributeError
class A:
descriptor = LogCountDescriptor()
a = A()
print("access count: {}".format(a.descriptor))
print("access count: {}".format(a.descriptor))
print("access count: {}".format(a.descriptor))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment