56 lines
1.1 KiB
Python
56 lines
1.1 KiB
Python
|
|
#!/usr/bin/env python
|
||
|
|
# -*- encoding: utf-8 -*-
|
||
|
|
"""
|
||
|
|
Topic: 延迟属性
|
||
|
|
Desc :
|
||
|
|
"""
|
||
|
|
import math
|
||
|
|
|
||
|
|
|
||
|
|
class lazyproperty:
|
||
|
|
def __init__(self, func):
|
||
|
|
self.func = func
|
||
|
|
|
||
|
|
def __get__(self, instance, cls):
|
||
|
|
if instance is None:
|
||
|
|
return self
|
||
|
|
else:
|
||
|
|
value = self.func(instance)
|
||
|
|
setattr(instance, self.func.__name__, value)
|
||
|
|
return value
|
||
|
|
|
||
|
|
|
||
|
|
class Circle:
|
||
|
|
def __init__(self, radius):
|
||
|
|
self.radius = radius
|
||
|
|
|
||
|
|
@lazyproperty
|
||
|
|
def area(self):
|
||
|
|
print('Computing area')
|
||
|
|
return math.pi * self.radius ** 2
|
||
|
|
|
||
|
|
@lazyproperty
|
||
|
|
def perimeter(self):
|
||
|
|
print('Computing perimeter')
|
||
|
|
return 2 * math.pi * self.radius
|
||
|
|
|
||
|
|
c = Circle(4.0)
|
||
|
|
print(vars(c))
|
||
|
|
print(c.area)
|
||
|
|
print(vars(c))
|
||
|
|
del c.area
|
||
|
|
print(vars(c))
|
||
|
|
print(c.area)
|
||
|
|
|
||
|
|
|
||
|
|
def lazyproperty2(func):
|
||
|
|
name = '_lazy_' + func.__name__
|
||
|
|
@property
|
||
|
|
def lazy(self):
|
||
|
|
if hasattr(self, name):
|
||
|
|
return getattr(self, name)
|
||
|
|
else:
|
||
|
|
value = func(self)
|
||
|
|
setattr(self, name, value)
|
||
|
|
return value
|
||
|
|
return lazy
|