python反射
isinstance: 判断对象是否是属于这个类(向上判断)
type: 返回某对象的数据类型
issubclass: 判断某类是否是这个类的子类
1 class Animal:
2 def chi(self):
3 print('吃饭')
4 class Cat(Animal):
5 def he(self):
6 print('喝水')
7 c = Cat()
8 print(isinstance(c,Cat)) #判断c是否是Cat的对象
9 print(isinstance(c,Animal)) #判断c是否是Animal的对象,只能向上判断
10 a = Animal()
11 print(isinstance(a,Cat)) #不能向下判断
12
13 精准的判断这个对象属于哪个类
14 print(type(a))
15 print(type(c))
16
17 判断某类是否是这个类的子类
18 print(issubclass(Cat,Animal))
19 print(issubclass(Animal,Cat))
20 结果
21 True
22 True
23 False
24 <class '__main__.Animal'>
25 <class '__main__.Cat'>
26 True
27 False
事例
1 def cul(a,b):
2 if (type(a) == int or type(a) == float) and (type(b) == int or type(b) == float):
3 return a + b
4 else:
5 return '无法计算'
6 print(cul('sad',13))
7 结果
8 无法计算
区分方法和函数(代码)
野路子: 打印的结果中包含了function的是函数,包含method的是方法
1 def func():
2 print('我是函数')
3 class Foo:
4 def chi(self):
5 print('吃饭')
6 print(func)
7 f = Foo()
8 f.chi()
9 print(f.chi)
10 结果
11 <function func at 0x0000025A57361E18>
12 吃饭
13 <bound method Foo.chi of <__main__.Foo object at 0x0000025A590573C8>>
在类中:(类也是对象)
实例方法
如果是类名.方法 函数
如果是对象.方法 方法
类方法: 都是方法
静态方法: 都是函数
1 class Person:
2 def chi(self):
3 print('我是实例方法')
4 @classmethod
5 def he(cls):
6 print('我是类方法')
7 @staticmethod
8 def wa():
9 print('我是静态方法')
10 p = Person()
11 print(p.chi)
12 Person.chi(1) # 不符合面向对象的思维(不建议用)
13 print(Person.chi)
14
15 类方法: 都是方法
16 print(Person.he)
17 print(p.he)
18
19 静态方法: 都是函数
20 print(Person.wa)
21 print(p.wa)
22 结果
23 <bound method Person.chi of <__main__.Person object at 0x000002437C2B73C8>>
24 我是实例方法
25 <function Person.chi at 0x000002437C2B98C8>
26 <bound method Person.he of <class '__main__.Person'>>
27 <bound method Person.he of <class '__main__.Person'>>
28 <function Person.wa at 0x000002437C2B99D8>
29 <function Person.wa at 0x000002437C2B99D8>
判断是否是方法或函数
1 from types import MethodType, FunctionType
2 isinstance()
3
4 from types import FunctionType,MethodType
5 class Person:
6 def chi(self):
7 print('我是实例方法')
8 @classmethod
9 def he(cls):
10 print('我是类方法')
11 @staticmethod
12 def wa():
13 print('我是静态方法')
14 p = Person()
15 print(isinstance(Person.chi,FunctionType))
16 print(isinstance(p.chi,MethodType))
17
18 print(isinstance(p.he,MethodType))
19 print(isinstance(Person.he,MethodType))
20
21 print(isinstance(p.wa,FunctionType))
22 print(isinstance(Person.wa,FunctionType))
23 结果
24 True
25 True
26 True
27 True
28 True
29 True
反射
对于模块而言可以使用getattr,hasattr,同样对于对象也可以执行类似的操作
getattr(): 从某对象中获取到某属性值
hasattr(): 判断某对象中是否有某属性值
1 在一个文件中写一个py文件
2 def chi():
3 print('吃饭')
4 def he():
5 print('喝水')
6
7 再在另外一个文件中判断
8 import test2
9 while 1:
10 content = input("<<:")
11 if hasattr(test2,content):
12 print('有这个功能')
13 ret = getattr(test2,content)
14 ret()
15 else:
16 print('没有这个功能')
17 结果
18 <<:chi
19 有这个功能
20 吃饭
21 <<:hh
22 没有这个功能
delattr(): 从某对象中删除某个属性
setattr(): 设置某对象中的某个属性为xxx
1 class Person:
2 def __init__(self,name,addr):
3 self.name = name
4 self.addr = addr
5 p = Person('jack','北京')
6 print(hasattr(p,'addr')) #判断这个属性是否存在
7 print(getattr(p,'addr')) #打印属性值
8 setattr(p,'addr','上海') #修改属性值p.addr='上海'
9 setattr(p,'money',9999999) #相当于新添加一个属性money=999999
10 print(p.addr)
11 print(p.money)
12 delattr(p,'addr')
13 # print(p.addr) #因为addr这个属性被删除了,所以打印报错
14 结果
15 True
16 北京
17 上海
18 9999999


浙公网安备 33010602011771号