Created
February 8, 2017 14:05
-
-
Save hryniuk/63ca8429ca9af2595f6a3fb3f4d9b7aa to your computer and use it in GitHub Desktop.
Python descriptor example
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
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