国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Python的descriptor(2)

2019-11-14 17:22:49
字體:
來源:轉載
供稿:網友

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

 

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

Python代碼  收藏代碼
  1. >>> def hello():  
  2.     pass  
  3.   
  4. >>> dir(hello)  
  5. ['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '<span style="color: #ff0000;">__get__</span>  
  6.   
  7.   
  8.   
  9. ', '__getattribute__',   
  10. '__hash__', '__init__', '__module__', '__name__', '__new__',   
  11. '__reduce__', '__reduce_ex__', '__re'__setattr__', '__str__', 'func_closure',   
  12. 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name']  
  13. >>>   

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

方法其實也是函數,如下:

  1. >>> class T(object):  
  2.     def hello(self):  
  3.         pass  
  4.   
  5. >>> T.__dict__['hello']  
  6. <function hello at 0x00CD7EB0>  
  7. >>>   

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

>>> T.hello   獲取T的hello屬性,根據查找策略,從T的__dict__中找到了,找到的是<function hello at 0x00CD7EB0>,但不會直接返回<function hello at 0x00CD7EB0>,因為它有__get__方法,所以返回的是調用它的__get__(None, T)的結果:一個unbound方法。<unbound method T.hello>>>> f = T.__dict__['hello']   #直接從T的__dict__中獲取hello,不會執行查找策略,直接返回了<function hello at 0x00CD7EB0>>>> f<function hello at 0x00CD7EB0>>>> t = T()                 >>> t.hello                     #從實例獲取屬性,返回的是調用<function hello at 0x00CD7EB0>的__get__(t, T)的結果:一個bound方法。<bound method T.hello of <__main__.T object at 0x00CDAD10>>>>> 

 為了證實我們上面的說法,在繼續下面的代碼(f還是上面的<function hello at 0x00CD7EB0>):

Python代碼  收藏代碼
  1. >>> f.__get__(None, T)  
  2. <unbound method T.hello>  
  3. >>> f.__get__(t, T)  
  4. <bound method T.hello of <__main__.T object at 0x00CDAD10>>  

 好極了!

 

總結一下:

      1.所有的函數都有__get__方法

      2.當函數位于類的__dict__中時,這個函數可以認為是個方法,通過類或實例獲取該函數時,返回的不是函數本身,而是它的__get__方法返回值。

 

我承認我可能誤導你認為方法就是函數,是特殊的函數。其實方法和函數還是有區別的,準確的說:方法就是方法,函數就是函數。

Python代碼  收藏代碼
  1. >>> type(f)  
  2. <type 'function'>  
  3. >>> type(t.hello)  
  4. <type 'instancemethod'>  
  5. >>> type(T.hello)  
  6. <type 'instancemethod'>  
  7. >>>   

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

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

Python代碼  收藏代碼
  1. >>> dir(t.hello)  
  2. ['__call__', '__class__', '__cmp__', '__delattr__', '__doc__', '__get__', '__getattribute__',   
  3. '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',   
  4. '__str__', 'im_class', 'im_func', 'im_self']  

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

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

 

其實還有幾個內建函數都和descriptor有關,下面簡單說說。

 

classmethod

 

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

Python代碼  收藏代碼
  1. >>> class T(object):  
  2.     def hello(cls):  
  3.         print 'hello', cls  
  4.     hello = classmethod(hello)   #兩個作用:把hello裝換成類方法,同時隱藏作為普通方法的hello  
  5.   
  6.       
  7. >>> t = T()  
  8. >>> t.hello()  
  9. hello <class '__main__.T'>  
  10. >>> T.hello()  
  11. hello <class '__main__.T'>  
  12. >>>   

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

Python代碼  收藏代碼
  1. >>> t.hello  
  2. <bound method type.hello of <class '__main__.T'>>  
  3. >>> type(t.hello)  
  4. <type 'instancemethod'>  
  5. >>> T.hello  
  6. <bound method type.hello of <class '__main__.T'>>  
  7. >>> type(T.hello)  
  8. <type 'instancemethod'>  
  9. >>>   

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

Python代碼  收藏代碼
  1. >>> t.hello.im_self  
  2. <class '__main__.T'>  
  3. >>> t.hello.im_class  
  4. <type 'type'>  
  5. >>> t.hello.im_func  
  6. <function hello at 0x011A40B0>  
  7. >>>   

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

 

staticmethod

 

staticmethod能將一個函數轉換成靜態方法,靜態方法沒有隱含的第一個參數。

Python代碼  收藏代碼
  1. class T(object):  
  2.     def hello():  
  3.         print 'hello'  
  4.     hello = staticmethod(hello)  
  5.   
  6.       
  7. >>> T.hello()   #沒有隱含的第一個參數  
  8. hello  
  9. >>> T.hello  
  10. <function hello at 0x011A4270>  
  11. >>>   

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

 

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


上一篇:使用python發送Email

下一篇:整理打印PI值

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 天等县| 长汀县| 和田县| 忻州市| 靖江市| 肃南| 阿拉善右旗| 兰州市| 岳普湖县| 汽车| 噶尔县| 柏乡县| 板桥市| 湖州市| 随州市| 牡丹江市| 延寿县| 盐池县| 西和县| 石河子市| 方山县| 冷水江市| 三明市| 义马市| 镇巴县| 宜良县| 读书| 白河县| 大足县| 眉山市| 墨竹工卡县| 鸡西市| 偏关县| 加查县| 霍邱县| 保靖县| 长垣县| 昭通市| 焉耆| 浏阳市| 宜州市|