在类中:
实例方法
如果是类名.方法 函数
如果是对象.方法 方法
类方法: 都是方法
静态方法: 都是函数
from types import MethodType, FunctionType
isinstance()
|
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# def func():# print("我是函数")## class Foo:# def chi(self):# print("我是吃")## # print(func) # <function func at 0x0000000001D42E18># f = Foo()# # f.chi()## print(f.chi) # <bound method Foo.chi of <__main__.Foo object at 0x0000000002894A90>>## # 野路子: 打印的结果中包含了function. 函数# # method . 方法## 我们的类也是对象.# 这个对象: 属性就是类变量# 方法就是类方法# class Person:# def chi(self):# print("我要吃鱼")## @classmethod# def he(cls):# print("我是类方法")## @staticmethod# def pi():# print("泥溪镇地皮")#### p = Person()# Person.chi(1) # 不符合面向对象的思维# print(p.chi) # <bound method Person.chi of <__main__.Person object at 0x00000000028C4B70>># print(Person.chi) # <function Person.chi at 0x00000000028989D8># 实例方法:# 1. 如果使用 对象.实例方法 方法# 2. 如果使用 类.实例方法 函数# print(Person.he) # <bound method Person.he of <class '__main__.Person'>># print(p.he) # <bound method Person.he of <class '__main__.Person'>># 类方法都是 方法# print(Person.pi) # <function Person.pi at 0x0000000009E7F488># print(p.pi) # <function Person.pi at 0x0000000009E7F488># 静态方法都是函数# 记下来from types import FunctionType, MethodType # 方法和函数from collections import Iterable, Iterator # 迭代器class Person: def chi(self): # 实例方法 print("我要吃鱼") @classmethod def he(cls): print("我是类方法") @staticmethod def pi(): print("泥溪镇地皮")p = Person()print(isinstance(Person.chi, FunctionType)) # Trueprint(isinstance(p.chi, MethodType)) # Trueprint(isinstance(p.he, MethodType)) # Trueprint(isinstance(Person.he, MethodType)) # Trueprint(isinstance(p.pi, FunctionType)) # Trueprint(isinstance(Person.pi, FunctionType)) # True |