#!/usr/bin/env python # -*- coding:utf-8 -*- import sys class WebServer(object): def __init__(self,host,port): self.host = host self.port = port def start(self): print("Server is starting....") def stop(self): print("Server is stoping....") def restart(self): self.stop() self.start() def test_run(ins,name): #把这个方法绑定到实例内,如下setattr例子是绑定到server实例内 setattr print("runing.,....",ins.host,name) if __name__ =="__main__": server = WebServer("local",2222) # print(sys.argv[1]) if hasattr(server,sys.argv[1]): #判断有没有 func = getattr(server,sys.argv[1]) # 获取server.start方法的内存地址 func() #server.start() # setattr(server,"run",test_run) # server.run(server,"alex") delattr(WebServer,"start") print(server.restart()) ''' cmd_dic = { 'start':server.start, "stop":server.stop } if sys.argv[1] in cmd_dic: cmd_dic[sys.argv[1]]() ''' #反射根据相应的字符串获取相应的内存地址