方法比较及说明
- 示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60class A(object):
def foo(self, x):
"""
普通方法
:param x:
:return:
"""
print "executing foo(%s,%s)" % (self, x)
def class_foo(cls, x):
"""
类方法
:param x:
:return:
"""
print "executing class_foo(%s,%s)" % (cls, x)
def static_foo(x):
"""
静态方法
:param x:
:return:
"""
print "executing static_foo(%s)" % x
a = A()
# 使用普通方法调用,第一个参数隐式传递给方法
a.foo(1)
# executing foo(<__main__.A object at 0xb7dbef0c>,1)
# 类调用普通方法
A.foo(1)
# 报错 unbound method foo() must be called with A instance as first argument (got int instance instead)
# 实例调用类方法使用
# 将隐式传递类本身给方法
a.class_foo(1)
# executing class_foo(<class '__main__.A'>,1)
# 类调用类方法
# 将方法定义为类方法,一般都是使用类直接调用
A.class_foo(1)
#静态方法 既不传递实例self也不传递类cls
#类调用静态方法
A.static_foo(1)
# executing static_foo(1)
# 实例调用静态方法
a.static_foo(1)
# executing static_foo(1)
静态方法将类的连接函数进行分组
- @classmethod
将方法转换成类方法,一个类方法接收 class 作为第一个参数,就像和实例方法接受实例一样。声明类方法,使用idiom:1
2
3class C:
@classmethod
def f(cls, arg1, arg2, ...): ...
@classmethod 是函数装饰器,他可以使用类调用和实例调用,如C.f() 和C().f(),除了类之外,该实例被忽略,如果派生类调用类方法,则派生类对象讲作为隐含的第一个参数传递
- @staticmethod
将方法转换成静态方法,
一个静态方法