1. 程式人生 > >python在類中實現swith case功能

python在類中實現swith case功能

開發十年,就只剩下這套架構體系了! >>>   

class RunMethod:
    def post(self,url=None,data=None,header=None):
        print(url)
    def get(self,url=None,data=None,header=None):
        print("get")

    def main(self,method):
        method = getattr(self, method)
        return method

if __name__ == '__main__':
    client = RunMethod()
    client.main("post")("http://www.baidu.com")

其中主要用到getattr這個函式,用於返回一個物件屬性值。

getattr(object, name[, default])
  • object -- 物件。
  • name -- 字串,物件屬性。
  • default -- 預設返回值,如果不提供該引數,在沒有對應屬性時,將觸發 Att