Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class LazyProperty(object):
- def __init__(self, func):
- self.func = func
- def __get__(self, instance, owner):
- if instance is None:
- return self
- else:
- value = self.func(instance)
- # 这里直接覆盖了类的func.__name__值,所以再次调用时不是调用类的func函数
- setattr(instance, self.func.__name__, value)
- return value
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement