国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - 用實例分析Python中method的參數傳遞過程

用實例分析Python中method的參數傳遞過程

2020-05-28 10:24伯樂在線acmerfight Python

這篇文章主要介紹了用實例分析Python中method的參數傳遞過程,包括instancemethod和staticmethod等實例,需要的朋友可以參考下

什么是method

function就是可以通過名字可以調用的一段代碼,我們可以傳參數進去,得到返回值。所有的參數都是明確的傳遞過去的。
method是function與對象的結合。我們調用一個方法的時候,有些參數是隱含的傳遞過去的。下文會詳細介紹。
instancemethod
 

?
1
2
3
4
5
6
7
8
9
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是一個沒有被綁定方法,什么叫做未綁定呢?繼續看下去。
 

?
1
2
3
4
5
6
7
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)

未綁定的方法必須使用一個Human實例作為第一個參數來調用啊。那我們來試試
 

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

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

?
1
2
3
4
In [11]: person = Human(45)
 
In [12]: person.get_weight()
Out[12]: 45

這兩種方式的結果一模一樣。我們看下官方文檔是怎么解釋這種現象的。
 
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.

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

?
1
2
3
4
5
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 這個實例對象上。
總結下

  1.     instance method 就是實例對象與函數的結合。
  2.     使用類調用,第一個參數明確的傳遞過去一個實例。
  3.     使用實例調用,調用的實例被作為第一個參數被隱含的傳遞過去。

classmethod
 

?
1
2
3
4
5
6
7
8
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是一個綁定在 Human 這個類上的method。調用下看看
 

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

類和類的實例都能調用 get_weight 而且調用結果完全一樣。
我們看到 weight 是屬于 Human 類的屬性,當然也是 Human 的實例的屬性。那傳遞過去的參數 cls 是類還是實例呢?
 

?
1
2
3
4
5
6
7
8
9
10
11
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 的實例,兩種方式調用的結果沒有任何區別。cls 只是一個普通的函數參數,調用時被隱含的傳遞過去。
總結起來

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

staticmethod
 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 在無論是類還是實例上都只是一個普通的函數,并沒有綁定在任何一個特定的類或者實例上。可以使用類或者類的實例調用,并且沒有任何隱含參數的傳入。
 

?
1
2
3
4
5
In [6]: Human().add is Human().add
Out[6]: True
 
In [7]: Human().get_weight is Human().get_weight
Out[7]: False

add 在兩個實例上也是同一個對象。instancemethod 就不一樣了,每次都會創建一個新的 get_weight 對象。
總結下

  1.     當一個函數邏輯上屬于一個類又不依賴與類的屬性的時候,可以使用 staticmethod。
  2.     使用 staticmethod 可以避免每次使用的時都會創建一個對象的開銷。
  3.     staticmethod 可以使用類和類的實例調用。但是不依賴于類和類的實例的狀態。

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 色五月激情综合网 | 国产亚洲一区二区精品 | 亚洲毛片在线观看 | 国产成人欧美一区二区三区的 | 欧美精品v国产精品v日韩精品 | 亚洲黄色在线观看 | 亚洲一区二区三区视频 | 综合精品久久久 | 欧美中文字幕一区二区 | 国产一区二区三区免费在线观看 | 亚洲视频一区 | 精品在线一区 | 久久精品xx老女人老配少 | 亚洲天堂一区在线 | 亚洲骚片| 国产精国产精品 | 成人av一级片| 国产在线一区二区 | 亚洲 欧美 日韩 在线 | 日韩欧美一区二区三区 | 久久国内| 午夜视频在线观看网站 | 亚洲欧美精品 | 男女中文字幕 | 色噜噜狠狠一区二区三区狼国成人 | 久久精品亚洲精品国产欧美kt∨ | 俺去俺来也在线www色官网 | 黄色一级电影在线观看 | 久久精品不卡 | 国产日韩欧美一区二区 | 久久久久99 | 亚洲免费一区二区 | 亚洲国产精品久久久久 | 一级毛片视频 | 久久精品99| 人人99| 日韩成人在线播放 | 性片网站| 高清国产午夜精品久久久久久 | 91av在线免费播放 | 91成人小视频|