猿问

Python中的类方法差异:绑定、未绑定和静态

Python中的类方法差异:绑定、未绑定和静态

下面的类方法有什么区别?

是一个是静态的,另一个不是静态的吗?

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()


狐的传说
浏览 507回答 3
3回答

catspeake

在Python中,定界和解束缚方法。基本上,对成员函数的调用(如method_one),一个有界函数a_test.method_one()翻译成Test.method_one(a_test)即对未绑定方法的调用。正因为如此,打电话给你的版本method_two将失败TypeError>>>&nbsp;a_test&nbsp;=&nbsp;Test()&nbsp;>>>&nbsp;a_test.method_two()Traceback&nbsp;(most&nbsp;recent&nbsp;call&nbsp;last): &nbsp;&nbsp;File&nbsp;"<stdin>",&nbsp;line&nbsp;1,&nbsp;in&nbsp;<module>TypeError:&nbsp;method_two()&nbsp;takes&nbsp;no&nbsp;arguments&nbsp;(1&nbsp;given)可以使用装饰器更改方法的行为。class&nbsp;Test(object): &nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;method_one(self): &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;"Called&nbsp;method_one" &nbsp;&nbsp;&nbsp;&nbsp;@staticmethod &nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;method_two(): &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;"Called&nbsp;method&nbsp;two"装饰师告诉内置的默认元类。type(一班中的一班,参见。这个问题)不创建绑定方法method_two.现在,您可以直接在实例或类上调用静态方法:>>>&nbsp;a_test&nbsp;=&nbsp;Test()>>>&nbsp;a_test.method_one()Called&nbsp;method_one>>>&nbsp;a_test.method_two()Called&nbsp;method_two>>>&nbsp;Test.method_two()Called&nbsp;method_two

aluckdog

一旦您了解了描述符系统的基本知识,Python中的方法就是一件非常简单的事情。想象一下以下课程:class&nbsp;C(object): &nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;foo(self): &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pass现在让我们看看shell中的类:>>>&nbsp;C.foo<unbound&nbsp;method&nbsp;C.foo>>>>&nbsp;C.__dict__['foo']<function&nbsp;foo&nbsp;at&nbsp;0x17d05b0>如您所见,如果您访问foo属性返回一个未绑定的方法,但是在类存储(Dict)中有一个函数。为什么这么说?这是因为类的类实现了__getattribute__这就解决了描述符。听起来很复杂,但事实并非如此。C.foo与此特殊情况下的代码大致相同:>>>&nbsp;C.__dict__['foo'].__get__(None,&nbsp;C)<unbound&nbsp;method&nbsp;C.foo>那是因为函数有一个__get__方法,使它们成为描述符。如果您有一个类的实例,它几乎是相同的,只是None是类实例:>>>&nbsp;c&nbsp;=&nbsp;C()>>>&nbsp;C.__dict__['foo'].__get__(c,&nbsp;C)<bound&nbsp;method&nbsp;C.foo&nbsp;of&nbsp;<__main__.C&nbsp;object&nbsp;at&nbsp;0x17bd4d0>>为什么Python会这么做呢?因为方法对象将函数的第一个参数绑定到类的实例。这就是赛尔夫的来历。有时候你不想让你的类把一个函数变成一个方法,这就是staticmethod发挥作用:&nbsp;class&nbsp;C(object): &nbsp;&nbsp;@staticmethod &nbsp;&nbsp;def&nbsp;foo(): &nbsp;&nbsp;&nbsp;pass这个staticmethod装饰器封装类并实现一个虚拟的__get__它将包装的函数作为函数而不是方法返回:>>>&nbsp;C.__dict__['foo'].__get__(None,&nbsp;C)<function&nbsp;foo&nbsp;at&nbsp;0x17d0c30>希望这就解释了。

杨__羊羊

>>> class Class(object):...&nbsp; &nbsp; &nbsp;def __init__(self):...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.i = 0...&nbsp; &nbsp; &nbsp;def instance_method(self):...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.i += 1...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print self.i...&nbsp; &nbsp; &nbsp;c = 0...&nbsp; &nbsp; &nbsp;@classmethod...&nbsp; &nbsp; &nbsp;def class_method(cls):...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cls.c += 1...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print cls.c...&nbsp; &nbsp; &nbsp;@staticmethod...&nbsp; &nbsp; &nbsp;def static_method(s):...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s += 1...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print s...&nbsp;>>> a = Class()>>> a.class_method()1>>> Class.class_method()&nbsp; &nbsp; # The class shares this value across instances2>>> a.instance_method()1>>> Class.instance_method() # The class cannot use an instance methodTraceback (most recent call last):&nbsp; 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&nbsp;>>>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # class or instance properties.3>>> Class.c&nbsp; &nbsp; &nbsp; &nbsp; # a.c above was passed by value and not by reference.2>>> a.c2>>> a.c = 5&nbsp; &nbsp; &nbsp; &nbsp; # The connection between the instance>>> Class.c&nbsp; &nbsp; &nbsp; &nbsp; # and its class is weak as seen here.2>>> Class.class_method()3>>> a.c5
随时随地看视频慕课网APP

相关分类

Python
我要回答