日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

Python 延迟初始化(lazy property)

發(fā)布時(shí)間:2025/5/22 python 185 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python 延迟初始化(lazy property) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Python 對象的延遲初始化是指,當(dāng)它第一次被創(chuàng)建時(shí)才進(jìn)行初始化,或者保存第一次創(chuàng)建的結(jié)果,然后每次調(diào)用的時(shí)候直接返回該結(jié)果。

延遲初始化主要用于提高性能,避免浪費(fèi)計(jì)算,并減少程序的內(nèi)存需求。

property

在切入正題之前,我們了解下property的用法,property可以將屬性的訪問轉(zhuǎn)變成方法的調(diào)用。

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雖然是定義成一個(gè)方法的形式,但是加上@property后,可以直接執(zhí)行c.area,當(dāng)成屬性訪問。

現(xiàn)在問題來了,每次調(diào)用c.area,都會(huì)計(jì)算一次,太浪費(fèi)cpu了,怎樣才能只計(jì)算一次呢?這就是lazy property。

lazy property

實(shí)現(xiàn)延遲初始化有兩種方式,一種是使用python描述符,另一種是使用@property修飾符。

方式1:

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

結(jié)果'evalute'只輸出了一次。在lazy類中,我們定義了__get__()方法,所以它是一個(gè)描述符。當(dāng)我們第一次執(zhí)行c.area時(shí),python解釋器會(huì)先從c.__dict__中進(jìn)行查找,沒有找到,就從Circle.__dict__中進(jìn)行查找,這時(shí)因?yàn)閍rea被定義為描述符,所以調(diào)用__get__方法。

在__get__()方法中,調(diào)用實(shí)例的area()方法計(jì)算出結(jié)果,并動(dòng)態(tài)給實(shí)例添加一個(gè)同名屬性area,然后將計(jì)算出的值賦予給它,相當(dāng)于設(shè)置c.__dict__['area']=val。

當(dāng)我們再次調(diào)用c.area時(shí),直接從c.__dict__中進(jìn)行查找,這時(shí)就會(huì)直接返回之前計(jì)算好的值了。

不太懂python描述符的話,可以參考Descriptor HowTo Guide。

方式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相當(dāng)于運(yùn)行以下代碼:

lazy_property(area)

lazy_property()方法返回_lazy_property,_lazy_property又會(huì)調(diào)用_lazy_property()方法,剩下的操作與方法1類似。

我們可以檢查下是否真的延遲初始化了:

c = Circle(4) print "before first visit" print c.__dict__ c.area print "after first visit" print c.__dict__

輸出結(jié)果為:

before first visit {'radius': 4} evalute after first visit {'_lazy_area': 50.24, 'radius': 4}

從中可以看書,只有當(dāng)我們第一次訪問c.area時(shí),才調(diào)用area方法,說明確實(shí)延遲初始化了。

參考文獻(xiàn)

  • Descriptor HowTo Guide

  • lazy evaluation

  • python中的property及實(shí)現(xiàn)lazy property(原博客已找不到)

總結(jié)

以上是生活随笔為你收集整理的Python 延迟初始化(lazy property)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。