反射

isinstance(x,y)  判断x是否是y的实例化对象

issubclass(a,b)判断a是否是b的子类

一、反射:

  指的是通过字符串来操作函数属性

   几个内置函数:

   hasattr(a,b):a对象是否存在b属性。本质是调用 in 关系运算

   getattr(a,b,c):获得a对象的b属性值,若不存在则返回c

   setattr(a,b,c):给a对象设置b属性,属性值为c、r

   delattr(a,b):删除a对象的b属性

   

class FTP:
    def get(self):
        print('get')

    def put(self):
        print('put')

    def login(self):
        print('login')

    def run(self):
        while True:
            choice = input('>>>').strip()
            if hasattr(self, choice):
                method = getattr(self, choice)
                method()
            else:
                print('命令不存在')

 

    

posted @ 2019-07-03 15:12  不吃海带丝  阅读(100)  评论(0编辑  收藏  举报