天天看點

Python自動化運維開發----基礎(十七)類的反射

需求:

因為最近在看python腳本腳本擷取oracle資料庫資料進行oracle資料庫監控,需要用到反射的方式做,去通過傳參調用不同函數去擷取不同的資料庫狀态(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()      

說明:輸入相應方法名會調用不用的方法

python類中的特殊方法