python3学习之反射的四种基本方法
生活随笔
收集整理的這篇文章主要介紹了
python3学习之反射的四种基本方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
class Person(object):
def __init__(self):
pass
def info(self):
print('我是person類中的info方法')
1.getattr()方法
這個方法是根據字符串去某個模塊中尋找方法
instantiation = reflect.Person()#先實例化 f = getattr(instantiation,'info')#使用getattr函數去尋找字符串的同名方法 f()#調用方法
輸出結果:我是person類中的info方法
2.hasattr()方法
這個方法是根據字符串去判斷某個模塊中該方法是否存在
instantiation = reflect.Person()#先實例化
f = hasattr(instantiation,'info') print(f) 輸出結果:True
3.setattr()方法
這個方法是根據字符串去某個模塊中設置方法
instantiation = reflect.Person() f = setattr(instantiation,'exit','this is a exit method') f2 = hasattr(instantiation,'exit') print(f2) 輸出結果就是True
4.delattr()方法
這個方法是根據字符串去某個模塊中刪除方法
instantiation = reflect.Person()#實例化 f = delattr(instantiation,'exit') f = hasattr(instantiation,'exit') print(f) 輸出結果就是False
總結
以上是生活随笔為你收集整理的python3学习之反射的四种基本方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Oracle+BEA后的ESB
- 下一篇: 万国觉醒腓特烈一世天赋点和英雄搭配