您好,登錄后才能下訂單哦!
需求:
因為最近在看python腳本腳本獲取oracle數(shù)據(jù)庫數(shù)據(jù)進行oracle數(shù)據(jù)庫監(jiān)控,需要用到反射的方式做,去通過傳參調(diào)用不同函數(shù)去獲取不同的數(shù)據(jù)庫狀態(tài)(python2)
#!/usr/bin/python class Exucutsql(object): def status(self): print("active") def db(self): print("100M") class Main(Exucutsql): def __init__(self): pass def __call__(self): method = raw_input("please input your method:") if hasattr(self,method): func = getattr(self, method) func() else: print("input method is not exits") if __name__ == "__main__": Main()
說明:輸入相應(yīng)方法名會調(diào)用不用的方法
python類中的特殊方法
__call__() 方法會當作一個函數(shù)去執(zhí)行
hasattr() 方法會判斷在類中是否存在方法
getattr() 方法會去調(diào)用執(zhí)行相應(yīng)的函數(shù)名方法(解決了過多的IF判斷的問題)
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。