欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

python基础教程:懒惰属性(延迟初始化)

发布时间:2025/3/20 python 32 豆豆
生活随笔 收集整理的这篇文章主要介绍了 python基础教程:懒惰属性(延迟初始化) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

Python 对象的延迟初始化是指,当它第一次被创建时才进行初始化,或者保存第一次创建的结果,然后每次调用的时候直接返回该结果。延迟初始化主要用于提高性能,避免浪费计算,并减少程序的内存需求。

1. 温故下property

property可以将属性的访问转变成方法的调用

class Circle(object): def __init__(self, radius): self.radius = radius @propertydef area(self): return 3.14 * self.radius ** 2c = Circle(4) print c.radius print c.area

可以看到,area虽然是定义成一个方法的形式,但是加上@property后,可以直接执行c.area,当成属性访问。

现在问题来了,每次调用c.area,都会计算一次,太浪费cpu了,怎样才能只计算一次呢?这就是lazy property

2.lazy property实现

实现延迟初始化有两种方式,一种是使用python描述符,另一种是使用@property修饰符

方法1:

''' 遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939 寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书! ''' class lazy(object): def __init__(self, func): self.func = func def __get__(self, instance, cls): val = self.func(instance) setattr(instance, self.func.__name__, val) return val class Circle(object): def __init__(self, radius): self.radius = radius @ lazydef area(self): print 'evalute'return 3.14 * self.radius ** 2c = Circle(4) print c.radius print c.area print c.area print 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__中进行查找,这时就会直接返回之前计算好的值了。

方法2:

def lazy_property(func):attr_name = "_lazy_" + func.__name__@propertydef _lazy_property(self):if not hasattr(self, attr_name):setattr(self, attr_name, func(self))return getattr(self, attr_name)return _lazy_propertyclass Circle(object): def __init__(self, radius): self.radius = radius @lazy_propertydef area(self): print 'evalute'return 3.14 * self.radius ** 2

这里与方法1异曲同工,在area()前添加@lazy_property相当于运行以下代码:

lazy_property(area)

lazy_property()方法返回_lazy_property,_lazy_property又会调用_lazy_property()方法,剩下的操作与方法1类似。

''' 遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939 寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书! ''' #性能差方法 class Circle(object): def __init__(self, radius): self.radius = radius @propertydef area(self): print("come in")return 3.14 * self.radius ** 2c = Circle(4) print(c.radius) print(c.area) print(c.area) #方法1 class LazyProperty:def __init__(self, method):self.method = methoddef __get__(self, instance, cls):if not instance:return Nonevalue = self.method(instance)setattr(instance,self.method.__name__,value)return valueclass Circle(object): def __init__(self, radius): self.radius = radius @LazyPropertydef area(self): print("come in")return 3.14 * self.radius ** 2c = Circle(4) print(c.radius) print(c.area) print(c.area)#方法2 def LazyProperty(func):attr_name = "_lazy_" + func.__name__@propertydef wrap(self):if not hasattr(self, attr_name):setattr(self, attr_name, func(self))return getattr(self, attr_name)return wrapclass Circle(object): def __init__(self, radius): self.radius = radius @LazyPropertydef area(self): print("come in")return 3.14 * self.radius ** 2c = Circle(4) print(c.radius) print(c.area) print(c.area)

总结

以上是生活随笔为你收集整理的python基础教程:懒惰属性(延迟初始化)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。