reflect

#!/usr/bin/env python
#-*-coding:utf8-*-
import sys
class Service(object):
    def __init__(self,name,ip,port):
        self.name=name
        self.ip=ip
        self.port=port
    def start(self):
       print('%s service is starting...'%self.name) 
    def stop(self):
       print('%s service stopped...'%self.name) 
    def restart(self):
       self.stop()
       self.start()
def customclassAttr(name):
    print('this is a customclassattribute',name) 
if __name__=='__main__':
    service1=Service('nginx','localhost',80)
    if hasattr(service1,sys.argv[1]):
        getattr(service1,sys.argv[1])()
    setattr(service1,'customclassattr',customclassAttr)#自定义类属性只属于service1实例的不属于类
    service1.customclassattr('testname')  
    delattr(service1,'name')#删除也只能删除service1该实例的属性或删除类的属性
    delattr(Service,'stop')
    service1.stop()
    print('service1 name...',service1.name)    

 

posted @ 2016-11-01 08:42  howhy  阅读(111)  评论(0)    收藏  举报