下面的类方法有什么区别?
是不是一个是静态的,另一个不是?
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()
下面的类方法有什么区别?
是不是一个是静态的,另一个不是?
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()
当前回答
The second one won't work because when you call it like that python internally tries to call it with the a_test instance as the first argument, but your method_two doesn't accept any arguments, so it wont work, you'll get a runtime error. If you want the equivalent of a static method you can use a class method. There's much less need for class methods in Python than static methods in languages like Java or C#. Most often the best solution is to use a method in the module, outside a class definition, those work more efficiently than class methods.
其他回答
Method_two不能工作,因为你定义了一个成员函数,但没有告诉它这个函数是什么成员。如果你执行最后一行,你会得到:
>>> a_test.method_two()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: method_two() takes no arguments (1 given)
如果你为一个类定义成员函数,第一个参数必须总是'self'。
一旦你了解了描述符系统的基础知识,Python中的方法就非常非常简单了。想象一下下面的类:
class C(object):
def foo(self):
pass
现在让我们来看看shell中的类:
>>> C.foo
<unbound method C.foo>
>>> C.__dict__['foo']
<function foo at 0x17d05b0>
正如你所看到的,如果你访问类上的foo属性,你会得到一个未绑定的方法,然而在类存储(dict)中有一个函数。为什么?这样做的原因是类的类实现了解析描述符的__getattribute__。听起来很复杂,但事实并非如此。在这种特殊情况下,C.foo大致相当于下面的代码:
>>> C.__dict__['foo'].__get__(None, C)
<unbound method C.foo>
这是因为函数有__get__方法,使它们成为描述符。如果你有一个类的实例,它几乎是一样的,只是None是类实例:
>>> c = C()
>>> C.__dict__['foo'].__get__(c, C)
<bound method C.foo of <__main__.C object at 0x17bd4d0>>
为什么Python要这么做呢?因为方法对象将函数的第一个形参绑定到类的实例。这就是自我的来源。有时候你不希望你的类把一个函数变成一个方法,这时就需要用到staticmethod了:
class C(object):
@staticmethod
def foo():
pass
staticmethod装饰器包装了你的类,并实现了一个虚拟__get__,它将被包装的函数作为函数而不是方法返回:
>>> C.__dict__['foo'].__get__(None, C)
<function foo at 0x17d0c30>
希望这能解释清楚。
The second one won't work because when you call it like that python internally tries to call it with the a_test instance as the first argument, but your method_two doesn't accept any arguments, so it wont work, you'll get a runtime error. If you want the equivalent of a static method you can use a class method. There's much less need for class methods in Python than static methods in languages like Java or C#. Most often the best solution is to use a method in the module, outside a class definition, those work more efficiently than class methods.
在Python中,绑定方法和未绑定方法是有区别的。
基本上,调用一个成员函数(比如method_one),一个绑定函数
a_test.method_one()
被翻译为
Test.method_one(a_test)
例如,调用一个未绑定的方法。正因为如此,调用你的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"
装饰器告诉内置的默认元类类型(类的类,参见这个问题)不要为method_two创建绑定方法。
现在,你可以直接在实例或类上调用静态方法:
>>> a_test = Test()
>>> a_test.method_one()
Called method_one
>>> a_test.method_two()
Called method_two
>>> Test.method_two()
Called method_two
当调用类成员时,Python自动使用对象引用作为第一个形参。变量self实际上没有任何意义,它只是一种编码约定。如果你愿意,可以叫它滴水怪。也就是说,对method_two的调用将引发TypeError,因为Python会自动尝试将一个参数(对其父对象的引用)传递给一个定义为没有参数的方法。
为了让它工作,你可以把这个附加到你的类定义:
method_two = staticmethod(method_two)
或者您可以使用@staticmethod函数装饰器。