亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

用實(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ù)名稱(chēng),不是關(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.     使用類(lèi)調(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è)類(lèi)上的method。調(diào)用下看看
 

In [3]: Human.get_weight()
Out[3]: 12
In [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)

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

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ú)論是類(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().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è)類(lèi)又不依賴(lài)與類(lèi)的屬性的時(shí)候,可以使用 staticmethod。
  2.     使用 staticmethod 可以避免每次使用的時(shí)都會(huì)創(chuàng)建一個(gè)對(duì)象的開(kāi)銷(xiāo)。
  3.     staticmethod 可以使用類(lèi)和類(lèi)的實(shí)例調(diào)用。但是不依賴(lài)于類(lèi)和類(lèi)的實(shí)例的狀態(tài)。

相關(guān)文章

最新評(píng)論