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

歡迎訪問 生活随笔!

生活随笔

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

python

python 描述器 详解_Python描述器descriptor详解

發(fā)布時間:2024/10/14 python 75 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 描述器 详解_Python描述器descriptor详解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

前面說了descriptor,這個東西其實和Java的setter,getter有點像。但這個descriptor和上文中我們開始提到的函數(shù)方法這些東西有什么關(guān)系呢?

所有的函數(shù)都可以是descriptor,因為它有__get__方法。

>>> def hello():

pass

>>> dir(hello)

['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__get__

', '__getattribute__',

'__hash__', '__init__', '__module__', '__name__', '__new__',

'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 'func_closure',

'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name']

>>>

注意,函數(shù)對象沒有__set__和__del__方法,所以它是個non-data descriptor.

方法其實也是函數(shù),如下:

>>> class T(object):

def hello(self):

pass

>>> T.__dict__['hello']

>>>

或者,我們可以把方法看成特殊的函數(shù),只是它們存在于類 中,獲取函數(shù)屬性時,返回的不是函數(shù)本身(比如上面的),而是返回函數(shù)的__get__方法的返回值,接著上面類T的定義:

>>> T.hello 獲取T的hello屬性,根據(jù)查找策略,從T的__dict__中找到了,找到的是,但不會直接返回,因為它有__get__方法,所以返回的是調(diào)用它的__get__(None, T)的結(jié)果:一個unbound方法。

>>> f = T.__dict__['hello'] #直接從T的__dict__中獲取hello,不會執(zhí)行查找策略,直接返回了

>>> f

>>> t = T()

>>> t.hello #從實例獲取屬性,返回的是調(diào)用的__get__(t, T)的結(jié)果:一個bound方法。

>

>>>

為了證實我們上面的說法,在繼續(xù)下面的代碼(f還是上面的):

>>> f.__get__(None, T)

>>> f.__get__(t, T)

>

好極了!

總結(jié)一下:

1.所有的函數(shù)都有__get__方法

2.當(dāng)函數(shù)位于類的__dict__中時,這個函數(shù)可以認(rèn)為是個方法,通過類或?qū)嵗@取該函數(shù)時,返回的不是函數(shù)本身,而是它的__get__方法返回值。

我承認(rèn)我可能誤導(dǎo)你認(rèn)為方法就是函數(shù),是特殊的函數(shù)。其實方法和函數(shù)還是有區(qū)別的,準(zhǔn)確的說:方法就是方法,函數(shù)就是函數(shù)。

>>> type(f)

>>> type(t.hello)

>>> type(T.hello)

>>>

函數(shù)是function類型的,method是instancemethod(這是普通的實例方法,后面會提到classmethod和staticmethod)。

關(guān)于unbound method和bound method,再多說兩句。在c實現(xiàn)中,它們是同一個對象(它們都是instancemethod類型的),我們先看看它們里面到底是什么

>>> dir(t.hello)

['__call__', '__class__', '__cmp__', '__delattr__', '__doc__', '__get__', '__getattribute__',

'__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',

'__str__', 'im_class', 'im_func', 'im_self']

__call__說明它們是個可調(diào)用對象,而且我們還可以猜測,這個__call__的實現(xiàn)應(yīng)該大致是:轉(zhuǎn)調(diào)另外一個函數(shù)(我們期望的哪個,比如上面的hello),并以對象作為第一參數(shù)。

要 注意的是im_class,im_func,im_self。這幾個東西我們并不陌生,在t.hello里,它們分別代表T,hello(這里是存儲在 T.__dict__里的函數(shù)hello)和t。有了這些我們可以大致想象如何純Python實現(xiàn)一個instancemethod了:)。

其實還有幾個內(nèi)建函數(shù)都和descriptor有關(guān),下面簡單說說。

classmethod

classmethod能將一個函數(shù)轉(zhuǎn)換成類方法,類方法的第一個隱含參數(shù)是類本身 (普通方法的第一個隱含參數(shù)是實例本身),類方法即可從類調(diào)用,也可以從實例調(diào)用(普通方法只能從實例調(diào)用)。

>>> class T(object):

def hello(cls):

print 'hello', cls

hello = classmethod(hello) #兩個作用:把hello裝換成類方法,同時隱藏作為普通方法的hello

>>> t = T()

>>> t.hello()

hello

>>> T.hello()

hello

>>>

注意:classmethod是個類,不是函數(shù)。classmethod類有__get__方法,所以,上面的t.hello和T.hello獲得實際上是classmethod的__get__方法返回值

>>> t.hello

>

>>> type(t.hello)

>>> T.hello

>

>>> type(T.hello)

>>>

從 上面可以看出,t.hello和T.hello是instancemethod類型的,而且是綁定在T上的。也就是說classmethod的 __get__方法返回了一個instancemethod對象。從前面對instancemethod的分析上,我們應(yīng)該可以推斷:t.hello的 im_self是T,im_class是type(T是type的實例),im_func是函數(shù)hello

>>> t.hello.im_self

>>> t.hello.im_class

>>> t.hello.im_func

>>>

完全一致!所以實現(xiàn)一個純Python的classmethod也不難:)

staticmethod

staticmethod能將一個函數(shù)轉(zhuǎn)換成靜態(tài)方法,靜態(tài)方法沒有隱含的第一個參數(shù)。

class T(object):

def hello():

print 'hello'

hello = staticmethod(hello)

>>> T.hello() #沒有隱含的第一個參數(shù)

hello

>>> T.hello

>>>

T.hello直接返回了一個函數(shù)。猜想staticmethod類的__get__方法應(yīng)該是直接返回了對象本身。

還有一個property,和上面兩個差不多,它是個data descriptor。

總結(jié)

以上是生活随笔為你收集整理的python 描述器 详解_Python描述器descriptor详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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