Last active
September 18, 2017 05:35
-
-
Save TennyZhuang/75f261fa73849e31e461ce0da3049afb to your computer and use it in GitHub Desktop.
Implement Python magic decorator
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 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