什么是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_weightOut[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ù)名稱(chēng),不是關(guān)鍵字。
In [13]: person.get_weightOut[13]: <bound method Human.get_weight of <__main__.Human object at 0x8e13bec>> In [14]: personOut[14]: <__main__.Human at 0x8e13bec>
我們看到get_weight被綁定在了 person 這個(gè)實(shí)例對(duì)象上。
總結(jié)下
classmethod
In [1]: class Human(object): ...: weight = 12 ...: @classmethod ...: def get_weight(cls): ...: return cls.weight In [2]: Human.get_weightOut[2]: <bound method type.get_weight of <class '__main__.Human'>>
我們看到get_weight是一個(gè)綁定在 Human 這個(gè)類(lèi)上的method。調(diào)用下看看
In [3]: Human.get_weight()Out[3]: 12In [4]: Human().get_weight()Out[4]: 12
類(lèi)和類(lèi)的實(shí)例都能調(diào)用 get_weight 而且調(diào)用結(jié)果完全一樣。
我們看到 weight 是屬于 Human 類(lèi)的屬性,當(dāng)然也是 Human 的實(shí)例的屬性。那傳遞過(guò)去的參數(shù) cls 是類(lèi)還是實(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 類(lèi),不是 Human 的實(shí)例,兩種方式調(diào)用的結(jié)果沒(méi)有任何區(qū)別。cls 只是一個(gè)普通的函數(shù)參數(shù),調(diào)用時(shí)被隱含的傳遞過(guò)去。
總結(jié)起來(lá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.addOut[2]: <function __main__.add> In [3]: Human().addOut[3]: <function __main__.add> In [4]: Human.add(1, 2)Out[4]: 3 In [5]: Human().add(1, 2)Out[5]: 3
我們看到 add 在無(wú)論是類(lèi)還是實(shí)例上都只是一個(gè)普通的函數(shù),并沒(méi)有綁定在任何一個(gè)特定的類(lèi)或者實(shí)例上。可以使用類(lèi)或者類(lèi)的實(shí)例調(diào)用,并且沒(méi)有任何隱含參數(shù)的傳入。
In [6]: Human().add is Human().addOut[6]: True In [7]: Human().get_weight is Human().get_weightOut[7]: False
add 在兩個(gè)實(shí)例上也是同一個(gè)對(duì)象。instancemethod 就不一樣了,每次都會(huì)創(chuàng)建一個(gè)新的 get_weight 對(duì)象。
總結(jié)下
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注