欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

什么是method?

function就是可以通過(guò)名字可以調(diào)用的一段代碼,我們可以傳參數(shù)進(jìn)去,得到返回值。所有的參數(shù)都是明確的傳遞過(guò)去的。
method是function與對(duì)象的結(jié)合。我們調(diào)用一個(gè)方法的時(shí)候,有些參數(shù)是隱含的傳遞過(guò)去的。下文會(huì)詳細(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è)沒(méi)有被綁定方法,什么叫做未綁定呢?繼續(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ù)來(lái)調(diào)用啊。那我們來(lái)試試
 

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.

原來(lái)我們常用的調(diào)用方法(person.get_weight())是把調(diào)用的實(shí)例隱藏的作為一個(gè)參數(shù)self傳遞過(guò)去了, 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í)例對(duì)象上。
總結(jié)下

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

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í)例的屬性。那傳遞過(guò)去的參數(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'>

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

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

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 在無(wú)論是類還是實(shí)例上都只是一個(gè)普通的函數(shù),并沒(méi)有綁定在任何一個(gè)特定的類或者實(shí)例上。可以使用類或者類的實(shí)例調(diào)用,并且沒(méi)有任何隱含參數(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è)對(duì)象。instancemethod 就不一樣了,每次都會(huì)創(chuàng)建一個(gè)新的 get_weight 對(duì)象。
總結(jié)下

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

相關(guān)文章

  • 對(duì)django2.0 關(guān)聯(lián)表的必填on_delete參數(shù)的含義解析

    對(duì)django2.0 關(guān)聯(lián)表的必填on_delete參數(shù)的含義解析

    今天小編就為大家分享一篇對(duì)django2.0 關(guān)聯(lián)表的必填on_delete參數(shù)的含義解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-08-08
  • Python?網(wǎng)易易盾滑塊驗(yàn)證功能的實(shí)現(xiàn)

    Python?網(wǎng)易易盾滑塊驗(yàn)證功能的實(shí)現(xiàn)

    這篇文章主要介紹了Python?網(wǎng)易易盾滑塊驗(yàn)證,主要是借助之前寫阿里云盾滑塊和極驗(yàn)滑塊的經(jīng)驗(yàn)寫的本文,通過(guò)使用selenium請(qǐng)求url,并觸發(fā)滑塊驗(yàn)證,需要的朋友可以參考下
    2022-05-05
  • Python3視頻轉(zhuǎn)字符動(dòng)畫的實(shí)例代碼

    Python3視頻轉(zhuǎn)字符動(dòng)畫的實(shí)例代碼

    這篇文章主要介紹了Python3視頻轉(zhuǎn)字符動(dòng)畫的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-08-08
  • Django自定義過(guò)濾器定義與用法示例

    Django自定義過(guò)濾器定義與用法示例

    這篇文章主要介紹了Django自定義過(guò)濾器定義與用法,結(jié)合實(shí)例形式分析了Django自定義過(guò)濾器的概念、功能、定義及使用方法,需要的朋友可以參考下
    2018-03-03
  • python如何在終端里面顯示一張圖片

    python如何在終端里面顯示一張圖片

    這篇文章主要為大家詳細(xì)介紹了python如何在終端里面顯示一張圖片的方法,感興趣的小伙伴們可以參考一下
    2016-08-08
  • 詳解Python各大聊天系統(tǒng)的屏蔽臟話功能原理

    詳解Python各大聊天系統(tǒng)的屏蔽臟話功能原理

    這篇文章主要介紹了詳解Python各大聊天系統(tǒng)的屏蔽臟話功能原理,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。
    2016-12-12
  • python中abs&map&reduce簡(jiǎn)介

    python中abs&map&reduce簡(jiǎn)介

    map與reduce是兩個(gè)十分常用的Python內(nèi)置函數(shù),它們與Hadoop中的MapReduce在某些方面有一定的相似之處。
    2018-02-02
  • python僵尸進(jìn)程產(chǎn)生的原因

    python僵尸進(jìn)程產(chǎn)生的原因

    這篇文章主要給大家講解的是在Python中是如何產(chǎn)生僵尸進(jìn)程的,以及如何清除僵尸進(jìn)程的方法,有需要的小伙伴可以參考下
    2017-07-07
  • python切片(獲取一個(gè)子列表(數(shù)組))詳解

    python切片(獲取一個(gè)子列表(數(shù)組))詳解

    這篇文章主要介紹了python切片(獲取一個(gè)子列表(數(shù)組))詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • Django中如何使用Channels功能

    Django中如何使用Channels功能

    這篇文章主要介紹了在Django中使用Channels功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-08-08

最新評(píng)論