最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

用實(shí)例分析Python中method的參數(shù)傳遞過程

 更新時(shí)間:2015年04月02日 16:45:06   作者:伯樂在線acmerfight  
這篇文章主要介紹了用實(shí)例分析Python中method的參數(shù)傳遞過程,包括instancemethod和staticmethod等實(shí)例,需要的朋友可以參考下

什么是method?

function就是可以通過名字可以調(diào)用的一段代碼,我們可以傳參數(shù)進(jìn)去,得到返回值。所有的參數(shù)都是明確的傳遞過去的。
method是function與對象的結(jié)合。我們調(diào)用一個(gè)方法的時(shí)候,有些參數(shù)是隱含的傳遞過去的。下文會詳細(xì)介紹。
instancemethod
 

In [5]: class Human(object):
  ...:   def __init__(self, weight):
  ...:     self.weight = weight
  ...:   def get_weight(self):
  ...:     return self.weight
  ...:  
 
In [6]: Human.get_weight
Out[6]: <unbound method Human.get_weight>

這告訴我們get_weight是一個(gè)沒有被綁定方法,什么叫做未綁定呢?繼續(xù)看下去。
 

In [7]: Human.get_weight()
---------------------------------------------------------------------------
TypeError                 Traceback (most recent call last)
/home/yao/learn/insight_python/<ipython-input-7-a2b2c5cd2f8d> in <module>()
----> 1 Human.get_weight()
 
TypeError: unbound method get_weight() must be called with Human instance as first argument (got nothing instead)

未綁定的方法必須使用一個(gè)Human實(shí)例作為第一個(gè)參數(shù)來調(diào)用啊。那我們來試試
 

In [10]: Human.get_weight(Human(45))
Out[10]: 45

果然成功了,但是一般情況下我們習(xí)慣這么使用。
 

In [11]: person = Human(45)
 
In [12]: person.get_weight()
Out[12]: 45

這兩種方式的結(jié)果一模一樣。我們看下官方文檔是怎么解釋這種現(xiàn)象的。
 
When an instance attribute is referenced that isn't a data attribute, its class is searched.
If the name denotes a valid class attribute that is a function object, a method object is
created by packing (pointers to) the instance object and the function object just found together
in an abstract object: this is the method object. When the method object is called with an
argument list, a new argument list is constructed from the instance object and the argument list,
and the function object is called with this new argument list.

原來我們常用的調(diào)用方法(person.get_weight())是把調(diào)用的實(shí)例隱藏的作為一個(gè)參數(shù)self傳遞過去了, self 只是一個(gè)普通的參數(shù)名稱,不是關(guān)鍵字。
 

In [13]: person.get_weight
Out[13]: <bound method Human.get_weight of <__main__.Human object at 0x8e13bec>>
 
In [14]: person
Out[14]: <__main__.Human at 0x8e13bec>

我們看到get_weight被綁定在了 person 這個(gè)實(shí)例對象上。
總結(jié)下

  1.     instance method 就是實(shí)例對象與函數(shù)的結(jié)合。
  2.     使用類調(diào)用,第一個(gè)參數(shù)明確的傳遞過去一個(gè)實(shí)例。
  3.     使用實(shí)例調(diào)用,調(diào)用的實(shí)例被作為第一個(gè)參數(shù)被隱含的傳遞過去。

classmethod
 

In [1]: class Human(object):
  ...:   weight = 12
  ...:   @classmethod
  ...:   def get_weight(cls):
  ...:     return cls.weight
 
In [2]: Human.get_weight
Out[2]: <bound method type.get_weight of <class '__main__.Human'>>

我們看到get_weight是一個(gè)綁定在 Human 這個(gè)類上的method。調(diào)用下看看
 

In [3]: Human.get_weight()
Out[3]: 12
In [4]: Human().get_weight()
Out[4]: 12

類和類的實(shí)例都能調(diào)用 get_weight 而且調(diào)用結(jié)果完全一樣。
我們看到 weight 是屬于 Human 類的屬性,當(dāng)然也是 Human 的實(shí)例的屬性。那傳遞過去的參數(shù) cls 是類還是實(shí)例呢?
 

In [1]: class Human(object):
  ...:   weight = 12
  ...:   @classmethod
  ...:   def get_weight(cls):
  ...:     print cls
 
In [2]: Human.get_weight()
<class '__main__.Human'>
 
In [3]: Human().get_weight()
<class '__main__.Human'>

我們看到傳遞過去的都是 Human 類,不是 Human 的實(shí)例,兩種方式調(diào)用的結(jié)果沒有任何區(qū)別。cls 只是一個(gè)普通的函數(shù)參數(shù),調(diào)用時(shí)被隱含的傳遞過去。
總結(jié)起來

  1.     classmethod 是類對象與函數(shù)的結(jié)合。
  2.     可以使用類和類的實(shí)例調(diào)用,但是都是將類作為隱含參數(shù)傳遞過去。
  3.     使用類來調(diào)用 classmethod 可以避免將類實(shí)例化的開銷。

staticmethod
 

In [1]: class Human(object):
  ...:   @staticmethod
  ...:   def add(a, b):
  ...:     return a + b
  ...:   def get_weight(self):
  ...:     return self.add(1, 2)
 
In [2]: Human.add
Out[2]: <function __main__.add>
 
In [3]: Human().add
Out[3]: <function __main__.add>
 
In [4]: Human.add(1, 2)
Out[4]: 3
 
In [5]: Human().add(1, 2)
Out[5]: 3

我們看到 add 在無論是類還是實(shí)例上都只是一個(gè)普通的函數(shù),并沒有綁定在任何一個(gè)特定的類或者實(shí)例上??梢允褂妙惢蛘哳惖膶?shí)例調(diào)用,并且沒有任何隱含參數(shù)的傳入。
 

In [6]: Human().add is Human().add
Out[6]: True
 
In [7]: Human().get_weight is Human().get_weight
Out[7]: False

add 在兩個(gè)實(shí)例上也是同一個(gè)對象。instancemethod 就不一樣了,每次都會創(chuàng)建一個(gè)新的 get_weight 對象。
總結(jié)下

  1.     當(dāng)一個(gè)函數(shù)邏輯上屬于一個(gè)類又不依賴與類的屬性的時(shí)候,可以使用 staticmethod。
  2.     使用 staticmethod 可以避免每次使用的時(shí)都會創(chuàng)建一個(gè)對象的開銷。
  3.     staticmethod 可以使用類和類的實(shí)例調(diào)用。但是不依賴于類和類的實(shí)例的狀態(tài)。

相關(guān)文章

  • python中np.multiply()、np.dot()和星號(*)三種乘法運(yùn)算的區(qū)別詳解

    python中np.multiply()、np.dot()和星號(*)三種乘法運(yùn)算的區(qū)別詳解

    這篇文章主要介紹了python中np.multiply()、np.dot()和星號(*)三種乘法運(yùn)算的區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • PyCharm安裝配置Qt Designer+PyUIC圖文教程

    PyCharm安裝配置Qt Designer+PyUIC圖文教程

    這篇文章主要介紹了PyCharm安裝配置Qt Designer+PyUIC圖文教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-05-05
  • python requests完成接口文件上傳的案例

    python requests完成接口文件上傳的案例

    這篇文章主要介紹了python requests完成接口文件上傳的案例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • 利用PyTorch實(shí)現(xiàn)爬山算法

    利用PyTorch實(shí)現(xiàn)爬山算法

    這篇文章主要介紹了利用PyTorch實(shí)現(xiàn)爬山算法,爬山算法是一種局部擇優(yōu)的方法,采用啟發(fā)式方法,是對深度優(yōu)先搜索的一種改進(jìn),它利用反饋信息幫助生成解的決策,屬于人工智能算法的一種
    2022-07-07
  • Python中五種實(shí)現(xiàn)字符串反轉(zhuǎn)的方法

    Python中五種實(shí)現(xiàn)字符串反轉(zhuǎn)的方法

    這篇文章主要介紹了Python中五種實(shí)現(xiàn)字符串反轉(zhuǎn)的方法,編寫一個(gè)函數(shù),其作用是將輸入的字符串反轉(zhuǎn)過來。下面文章關(guān)于其詳細(xì)介紹,需要的小伙伴可以參考一下
    2022-05-05
  • python學(xué)習(xí)之編寫查詢ip程序

    python學(xué)習(xí)之編寫查詢ip程序

    這篇文章主要介紹了python學(xué)習(xí)之編寫查詢ip程序 ,需要的朋友可以參考下
    2016-02-02
  • Python使用OpenCV和K-Means聚類對畢業(yè)照進(jìn)行圖像分割

    Python使用OpenCV和K-Means聚類對畢業(yè)照進(jìn)行圖像分割

    圖像分割是將圖像分割成多個(gè)不同區(qū)域(或片段)的過程。目標(biāo)是將圖像的表示變成更容易和更有意義的圖像。在這篇博客中,我們詳細(xì)的介紹了使用方法,感興趣的可以了解一下
    2021-06-06
  • Python3中urlencode和urldecode的用法詳解

    Python3中urlencode和urldecode的用法詳解

    今天小編就為大家分享一篇Python3中urlencode和urldecode的用法詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • 更換Django默認(rèn)的模板引擎為jinja2的實(shí)現(xiàn)方法

    更換Django默認(rèn)的模板引擎為jinja2的實(shí)現(xiàn)方法

    今天小編就為大家分享一篇更換Django默認(rèn)的模板引擎為jinja2的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • pytorch和numpy默認(rèn)浮點(diǎn)類型位數(shù)詳解

    pytorch和numpy默認(rèn)浮點(diǎn)類型位數(shù)詳解

    這篇文章主要介紹了pytorch和numpy默認(rèn)浮點(diǎn)類型位數(shù),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02

最新評論

高邑县| 根河市| 安乡县| 顺义区| 德安县| 天津市| 朝阳区| 应城市| 九龙县| 康定县| 兰溪市| 龙游县| 河源市| 嘉祥县| 阿巴嘎旗| 许昌市| 民县| 息烽县| 井陉县| 大渡口区| 楚雄市| 盐城市| 浦江县| 远安县| 墨玉县| 乌海市| 屏南县| 喜德县| 墨竹工卡县| 外汇| 包头市| 加查县| 西安市| 商城县| 平湖市| 六安市| 寿宁县| 香港| 庆云县| 鸡东县| 阳原县|