类的反射器:
python class.py sys.argv[1]
#/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 %s is starting‘%self.host) def stop(self): print(‘server %s is stoping‘%self.host) def restart(self): self.start() self.stop() if __name__ == "__main__": server = webserver(‘localhost‘,‘333‘) if hasattr(server,sys.argv[1]): #判断参数是否在实例中 func = getattr(server,sys.argv[1])#获取实例的内存地址 func()#执行实例的方法可以传参数
时间: 2024-10-18 19:38:41