Skip to content

Commit 1b8f3ae

Browse files
authored
Update class_intrduction.md
1 parent 3ce15b8 commit 1b8f3ae

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

class_intrduction.md

+13
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,16 @@
55

66
## python classmethod
77
类方法把一个类作为第一个接受的函数值,可以访问class内部的变量但不能访问实例变量
8+
9+
普通方法(simple)类方法(classmethod)静态方法(staticmethod)的区别只需要抓住这一点他们都写在class类内.```
10+
class A(object):
11+
def f(self, x):
12+
pass
13+
@classmethod
14+
def f(cls, x):
15+
pass
16+
@staticmethod
17+
def f(x):
18+
passa = A()
19+
```
20+
所以他们唯一的区别是函数的第一个参数绑定的对象不一样:普通方法(simple) def f(self, x): 的第一个参数 self 绑定的对象是实例对象 a, 第二个参数是 x ;类方法(classmethod) def f(cls, x): 的第一个参数 cls 绑定的对象是类 A , 第二个参数是 x ;静态方法(staticmethod) def f(x): 的第一个参数就是传参 x 自己.

0 commit comments

Comments
 (0)