用實(shí)例分析Python中method的參數(shù)傳遞過(guò)程
什么是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é)下
- instance method 就是實(shí)例對(duì)象與函數(shù)的結(jié)合。
- 使用類調(diào)用,第一個(gè)參數(shù)明確的傳遞過(guò)去一個(gè)實(shí)例。
- 使用實(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)
- classmethod 是類對(duì)象與函數(shù)的結(jié)合。
- 可以使用類和類的實(shí)例調(diào)用,但是都是將類作為隱含參數(shù)傳遞過(guò)去。
- 使用類來(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é)下
- 當(dāng)一個(gè)函數(shù)邏輯上屬于一個(gè)類又不依賴與類的屬性的時(shí)候,可以使用 staticmethod。
- 使用 staticmethod 可以避免每次使用的時(shí)都會(huì)創(chuàng)建一個(gè)對(duì)象的開(kāi)銷。
- staticmethod 可以使用類和類的實(shí)例調(diào)用。但是不依賴于類和類的實(shí)例的狀態(tài)。
- Python函數(shù)可變參數(shù)定義及其參數(shù)傳遞方式實(shí)例詳解
- Python def函數(shù)的定義、使用及參數(shù)傳遞實(shí)現(xiàn)代碼
- Python 詳解基本語(yǔ)法_函數(shù)_返回值
- Python簡(jiǎn)單調(diào)用MySQL存儲(chǔ)過(guò)程并獲得返回值的方法
- Python函數(shù)返回值實(shí)例分析
- Python中用函數(shù)作為返回值和實(shí)現(xiàn)閉包的教程
- 深入理解python中的淺拷貝和深拷貝
- Python中的深拷貝和淺拷貝詳解
- Python中的賦值、淺拷貝、深拷貝介紹
- Python對(duì)象的深拷貝和淺拷貝詳解
- Python 拷貝對(duì)象(深拷貝deepcopy與淺拷貝copy)
- Python 中的參數(shù)傳遞、返回值、淺拷貝、深拷貝
相關(guān)文章
對(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-08Python?網(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-05Python3視頻轉(zhuǎn)字符動(dòng)畫的實(shí)例代碼
這篇文章主要介紹了Python3視頻轉(zhuǎn)字符動(dòng)畫的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08python切片(獲取一個(gè)子列表(數(shù)組))詳解
這篇文章主要介紹了python切片(獲取一個(gè)子列表(數(shù)組))詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08