Python高级技巧:lazy property
Python 对象的延迟初始化是指,当它第一次被创建时才进行初始化,或者保存第一次创建的结果,然后每次调用的时候直接返回该结果。延迟初始化主要用于提高性能,避免浪费计算,并减少程序的内存需求。
property
在切入正题之前,我们了解下property的用法,property可以将属性的访问转变成方法的调用。
1classCircle(object):
2def__init__(self, radius):
3
4
5
6defarea(self):
7return3.14
8
9
10print
11print
2def__init__(self, radius):
3
self.radius = radius
4
5
@property
6defarea(self):
7return3.14
* self.radius **
28
9
c = Circle(
4)
10print
c.radius
11print
c.area
可以看到,area虽然是定义成一个方法的形式,但是加上@property后,可以直接执行c.area,当成属性访问。
现在问题来了,每次调用c.area,都会计算一次,太浪费cpu了,怎样才能只计算一次呢?这就是lazy property。
lazy property
实现延迟初始化有两种方式,一种是使用python描述符,另一种是使用@property修饰符。
方式1:
1classlazy(object):
2def__init__(self, func):
3
4
5def__get__(self, instance, cls):
6
7
8return
9
10classCircle(object):
11def__init__(self, radius):
12
13
14
15defarea(self):
16print'evalute'
17return3.14
18
19
20print
21print
22print
23print
2def__init__(self, func):
3
self.func = func
4
5def__get__(self, instance, cls):
6
val = self.func(instance)
7
setattr(instance, self.func.__name__, val)
8return
val
9
10classCircle(object):
11def__init__(self, radius):
12
self.radius = radius
13
14
@lazy
15defarea(self):
16print'evalute'
17return3.14
* self.radius **
218
19
c = Circle(
4)
20print
c.radius
21print
c.area
22print
c.area
23print
c.area
结果'evalute'只输出了一次。在lazy类中,我们定义了get__()方法,所以它是一个描述符。当我们第一次执行c.area时,python解释器会先从c._dict_中进行查找,没有找到,就从Circle._dict_中进行查找,这时因为area被定义为描述符,所以调用__get方法。
在get__()方法中,调用实例的area()方法计算出结果,并动态给实例添加一个同名属性area,然后将计算出的值赋予给它,相当于设置c.__dict['area']=val。
当我们再次调用c.area时,直接从c.dict中进行查找,这时就会直接返回之前计算好的值了。
不太懂python描述符的话,可以参考Descriptor HowTo Guide
方式2
1deflazy_property(func):
2
3
4
5def_lazy_property(self):
6ifnot
7
8return
9
10return
11
12classCircle(object):
13def__init__(self, radius):
14
15
16
17defarea(self):
18print'evalute'
19return3.14
2
attr_name =
"_lazy_" + func.__name__
3
4
@property
5def_lazy_property(self):
6ifnot
hasattr(self, attr_name):
7
setattr(self, attr_name, func(self))
8return
getattr(self, attr_name)
9
10return
_lazy_property
11
12classCircle(object):
13def__init__(self, radius):
14
self.radius = radius
15
16
@lazy_property
17defarea(self):
18print'evalute'
19return3.14
* self.radius **
2这里与方法1异曲同工,在area()前添加@lazy_property相当于运行以下代码:
1
lazy_property(area)
lazy_property()方法返回_lazy_property,_lazy_property又会调用_lazy_property()方法,剩下的操作与方法1类似。
我们可以检查下是否真的延迟初始化了:
1
2print"before first visit"
3print
4
5print"after first visit"
6print
7
8#输出结果为:
9
10
11
12
13
14
c = Circle(
4)
2print"before first visit"
3print
c.__dict__
4
c.area
5print"after first visit"
6print
c.__dict__
7
8#输出结果为:
9
10
before first visit
11
{
'radius':
4}
12
evalute
13
after first visit
14
{
'_lazy_area':
50.24,
'radius':
4}
推荐阅读
最新评论
推荐文章
作者最新文章
你可能感兴趣的文章
Copyright Disclaimer: The copyright of contents (including texts, images, videos and audios) posted above belong to the User who shared or the third-party website which the User shared from. If you found your copyright have been infringed, please send a DMCA takedown notice to [email protected]. For more detail of the source, please click on the button "Read Original Post" below. For other communications, please send to [email protected].
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。