Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.41 KB | None | 0 0
  1. class LazyProperty(object):
  2.  
  3. def __init__(self, func):
  4. self.func = func
  5.  
  6. def __get__(self, instance, owner):
  7. if instance is None:
  8. return self
  9. else:
  10. value = self.func(instance)
  11. # 这里直接覆盖了类的func.__name__值,所以再次调用时不是调用类的func函数
  12. setattr(instance, self.func.__name__, value)
  13. return value
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement