Skip to content

Instantly share code, notes, and snippets.

@TennyZhuang
Last active September 18, 2017 05:35
Show Gist options
  • Save TennyZhuang/75f261fa73849e31e461ce0da3049afb to your computer and use it in GitHub Desktop.
Save TennyZhuang/75f261fa73849e31e461ce0da3049afb to your computer and use it in GitHub Desktop.
Implement Python magic decorator
class PropertyDescriptor():
def __init__(self, getmethod):
self.getmethod = getmethod
def __get__(self, obj, cls=None):
return self.getmethod(obj)
def my_property(f):
return PropertyDescriptor(f)
class A(object):
@my_property
def c(self):
print('visit c')
return 1
b = 3
a = A()
print(a.c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment