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
複製代碼

事例spa

複製代碼
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的是方法code

複製代碼
 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>
複製代碼

判斷是不是方法或函數blog

複製代碼
 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(): 判斷某對象中是否有某屬性值get

複製代碼
 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(): 設置某對象中的某個屬性爲xxxinput

複製代碼
 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
相關文章
相關標籤/搜索