python3-----反射实例

#/usr/bin/env python
# -*- coding:utf-8 -*-
import sys,os
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,self):
    print('running...',name,self.host)

if __name__ == "__main__":
    server = WebServer('localhost',333)
    server2 = WebServer('localhost',333)
    #if hasattr(server,sys.argv[1]):
    #    func = getattr(server,sys.argv[1])  #获取server.start的内存地址
    #    func()
    setattr(server,'run',test_run)
    server.run('qinxiaowei',server)
   #delattr(server,"host")
    print(server.host)

#主要介绍了 hasattr ,getattr,delattr的使用

 

posted @ 2017-02-27 22:12  DevOps_QXW  阅读(622)  评论(0编辑  收藏  举报