发布时间:2019-07-17 10:08:44编辑:auto阅读(1447)
python学习反射
实例1
脚本内容如下:
#coding: utf8 class myClass(object): def fax(self): print 'faxing....' def copy(self): print 'copying...' def printing(): print 'printing....' m = myClass() if hasattr(m,'fax'): #判断myClass类中是否有fax方法 func = getattr(m,'fax') #返回内存对象 func() #调用 setattr(m,'print2',printing) m.print2() try: #delattr(m,'copy') #print name #print dfdf #print tttt print m except AttributeError,e: print 'something wrong..',e except Exception,e: print e finally: print '-----------' #else: # print "print nothing wrong ...." |
执行脚本结果如下:
faxing....
printing....
<__main__.myClass object at 0x028211D0>
-----------
实例2
脚本内容如下:
#/usr/bin/env python #coding: utf8 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 stopping..." def restart(self): self.stop() self.start() if __name__ == "__main__": server = WebServer('localhost',80) #print sys.argv[1] cmd_dic = { 'start':server.start, 'stop':server.stop, 'restart':server.restart, } #if sys.argv[1] == 'start': if sys.argv[1] in cmd_dic: cmd_dic[sys.argv[1]]() |
执行脚本结果如下:
D:\Python学习\脚本学习>python 反射01.py start
Server is starting...
修改上面的脚本如下:
#/usr/bin/env python #coding: utf8 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 stopping..." def restart(self): self.stop() self.start() if __name__ == "__main__": server = WebServer('localhost',80) if hasattr(server,sys.argv[1]): func = getattr(server,sys.argv[1]) ##获取server.start的内存地址 func() ##相当于server.start() |
执行脚本结果如下:
D:\Python学习\脚本学习>python 反射02.py start
Server is starting...
D:\Python学习\脚本学习>python 反射02.py stop
Server is stopping...
修改上面的脚本如下:
#/usr/bin/env python #coding: utf8 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 stopping..." def restart(self): self.stop() self.start() def test_run(name): print "running...",name if __name__ == "__main__": server = WebServer('localhost',80) if hasattr(server,sys.argv[1]): func = getattr(server,sys.argv[1]) ##获取server.start的内存地址 func() ##相当于server.start() setattr(server,'run',test_run) ##test_run以run身份绑定到server实例中 server.run('peng') ##类似执行test_run('peng') |
执行脚本结果如下:
D:\Python学习\脚本学习>python 反射03.py start
Server is starting...
running... peng
修改上面的脚本如下:
#/usr/bin/env python #coding: utf8 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 stopping..." def restart(self): self.stop() self.start() def test_run(self,name): print "running...",name,self.host if __name__ == "__main__": server = WebServer('localhost',80) if hasattr(server,sys.argv[1]): func = getattr(server,sys.argv[1]) ##获取server.start的内存地址 func() ##相当于server.start() setattr(server,'run',test_run) ##test_run以run身份绑定到server实例中 server.run(server,'peng') ##类似执行test_run('peng') |
执行脚本结果如下:
D:\Python学习\脚本学习>python 反射04.py start
Server is starting...
running... peng localhost
修改上面的脚本如下:
#/usr/bin/env python #coding: utf8 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 stopping..." def restart(self): self.stop() self.start() def test_run(self,name): print "running...",name,self.host if __name__ == "__main__": server = WebServer('localhost',80) if hasattr(server,sys.argv[1]): func = getattr(server,sys.argv[1]) ##获取server.start的内存地址 func() ##相当于server.start() delattr(WebServer,'start') print server.restart() #setattr(server,'run',test_run) ##test_run以run身份绑定到server实例中 #server.run(server,'peng') ##类似执行test_run('peng') |
执行脚本结果如下:
D:\Python学习\脚本学习>python 反射05.py restart
Server is stopping... ---》执行的是func()的调用
Server is starting... ---》执行的是func()的调用
Server is stopping... ---》执行的是server.restart()的调用
Traceback (most recent call last):
File "反射05.py", line 31, in <module>
print server.restart()
File "反射05.py", line 19, in restart
self.start()
AttributeError: 'WebServer' object has no attribute 'start' ---》执行的是server.restart()的调用,此时start已经删除,所以报了“'WebServer' object has no attribute 'start'”错误!!!
上一篇: python import 与 from
下一篇: Python的控制流
47484
45786
36784
34317
28955
25589
24436
19606
19102
17627
5458°
6041°
5563°
5632°
6565°
5370°
5370°
5878°
5851°
7163°