所謂類屬性的延遲計算就是將類的屬性定義成一個property,只在訪問的時候才會計算,而且一旦被訪問后,結果將會被緩存起來,不用每次都計算。構造一個延遲計算屬性的主要目的是為了提升性能
property
在切入正題之前,我們了解下property的用法,property可以將屬性的訪問轉變成方法的調用。
1
2
3
4
5
6
7
8
9
10
11
|
class Circle( object ): def __init__( self , radius): self .radius = radius @property def area( self ): return 3.14 * self .radius * * 2 c = Circle( 4 ) print c.radius print c.area |
可以看到,area雖然是定義成一個方法的形式,但是加上@property后,可以直接執行c.area,當成屬性訪問。
現在問題來了,每次調用c.area,都會計算一次,太浪費cpu了,怎樣才能只計算一次呢?這就是lazy property
代碼實現
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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) setattr (instance, self .func.__name__, value) return value import math class Circle( object ): 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 |
說明
定義了一個延遲計算的裝飾器類LazyProperty。Circle是用于測試的類,Circle類有是三個屬性半徑(radius)、面積(area)、周長(perimeter)。面積和周長的屬性被LazyProperty裝飾,下面來試試LazyProperty的魔法:
1
2
3
4
5
6
|
>>> c = Circle( 2 ) >>> print c.area Computing area 12.5663706144 >>> print c.area 12.5663706144 |
在area()中每計算一次就會打印一次“Computing area”,而連續調用兩次c.area后“Computing area”只被打印了一次。這得益于LazyProperty,只要調用一次后,無論后續調用多少次都不會重復計算。