反射

import sys
class
Nginx(object): def __init__(self,name): self.name=name def start(self): print('%s server is starting...' %self.name) def stop(self): print('%s server stop...' %self.name) def restart(self): self.stop() self.start()
def myaction(name):
  print("%s server status" %name)
def __name__=='__main__': server=Nginx('nginx') if hasattr(server,sys.argv[1]): action=getattr(server,sys.argv[1]) action()
   setattr(server,'action',myaction)
  server.action('nginx')
   delattr(server,'action')
   delattr(server,'name')
   delattr(Nginx,'restart')
  

 

posted @ 2016-08-09 21:52  howhy  阅读(81)  评论(0编辑  收藏  举报