第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python中的類方法差異:綁定、未綁定和靜態(tài)

Python中的類方法差異:綁定、未綁定和靜態(tài)

狐的傳說 2019-07-09 16:33:14
Python中的類方法差異:綁定、未綁定和靜態(tài)下面的類方法有什么區(qū)別?是一個是靜態(tài)的,另一個不是靜態(tài)的嗎?class Test(object):   def method_one(self):     print "Called method_one"   def method_two():     print "Called method_two"a_test = Test()a_test.method_one()a_test.method_two()
查看完整描述

3 回答

?
catspeake

TA貢獻1111條經(jīng)驗 獲得超0個贊

在Python中,定界解束縛方法。

基本上,對成員函數(shù)的調(diào)用(如method_one),一個有界函數(shù)

a_test.method_one()

翻譯成

Test.method_one(a_test)

即對未綁定方法的調(diào)用。正因為如此,打電話給你的版本method_two將失敗TypeError

>>> a_test = Test() >>> a_test.method_two()Traceback (most recent call last):
  File "<stdin>", line 1, in <module>TypeError: method_two() takes no arguments (1 given)

可以使用裝飾器更改方法的行為。

class Test(object):
    def method_one(self):
        print "Called method_one"

    @staticmethod
    def method_two():
        print "Called method two"

裝飾師告訴內(nèi)置的默認元類。type(一班中的一班,參見。這個問題)不創(chuàng)建綁定方法method_two.

現(xiàn)在,您可以直接在實例或類上調(diào)用靜態(tài)方法:

>>> a_test = Test()>>> a_test.method_one()Called method_one>>> a_test.method_two()Called method_two>>> Test.method_two()Called method_two


查看完整回答
反對 回復 2019-07-09
?
aluckdog

TA貢獻1847條經(jīng)驗 獲得超7個贊

一旦您了解了描述符系統(tǒng)的基本知識,Python中的方法就是一件非常簡單的事情。想象一下以下課程:

class C(object):
    def foo(self):
        pass

現(xiàn)在讓我們看看shell中的類:

>>> C.foo<unbound method C.foo>>>> C.__dict__['foo']<function foo at 0x17d05b0>

如您所見,如果您訪問foo屬性返回一個未綁定的方法,但是在類存儲(Dict)中有一個函數(shù)。為什么這么說?這是因為類的類實現(xiàn)了__getattribute__這就解決了描述符。聽起來很復雜,但事實并非如此。C.foo與此特殊情況下的代碼大致相同:

>>> C.__dict__['foo'].__get__(None, C)<unbound method C.foo>

那是因為函數(shù)有一個__get__方法,使它們成為描述符。如果您有一個類的實例,它幾乎是相同的,只是None是類實例:

>>> c = C()>>> C.__dict__['foo'].__get__(c, C)<bound method C.foo of <__main__.C object at 0x17bd4d0>>

為什么Python會這么做呢?因為方法對象將函數(shù)的第一個參數(shù)綁定到類的實例。這就是賽爾夫的來歷。有時候你不想讓你的類把一個函數(shù)變成一個方法,這就是staticmethod發(fā)揮作用:

 class C(object):
  @staticmethod
  def foo():
   pass

這個staticmethod裝飾器封裝類并實現(xiàn)一個虛擬的__get__它將包裝的函數(shù)作為函數(shù)而不是方法返回:

>>> C.__dict__['foo'].__get__(None, C)<function foo at 0x17d0c30>

希望這就解釋了。


查看完整回答
反對 回復 2019-07-09
?
楊__羊羊

TA貢獻1943條經(jīng)驗 獲得超7個贊

>>> class Class(object):

...     def __init__(self):

...         self.i = 0

...     def instance_method(self):

...         self.i += 1

...         print self.i

...     c = 0

...     @classmethod

...     def class_method(cls):

...         cls.c += 1

...         print cls.c

...     @staticmethod

...     def static_method(s):

...         s += 1

...         print s

... 

>>> a = Class()

>>> a.class_method()

1

>>> Class.class_method()    # The class shares this value across instances

2

>>> a.instance_method()

1

>>> Class.instance_method() # The class cannot use an instance method

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: unbound method instance_method() must be called with Class instance as first argument (got nothing instead)

>>> Class.instance_method(a)

2

>>> b = 0

>>> a.static_method(b)

1

>>> a.static_method(a.c) # Static method does not have direct access to 

>>>                      # class or instance properties.

3

>>> Class.c        # a.c above was passed by value and not by reference.

2

>>> a.c

2

>>> a.c = 5        # The connection between the instance

>>> Class.c        # and its class is weak as seen here.

2

>>> Class.class_method()

3

>>> a.c

5


查看完整回答
反對 回復 2019-07-09
  • 3 回答
  • 0 關(guān)注
  • 534 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號