Python正课80 —— 反射

本文内容皆为作者原创,如需转载,请注明出处:https://www.cnblogs.com/xuexianqi/p/12703295.html

一:什么是反射

指的是在程序运行过程中可以“动态(不见棺材不落泪)”获取对象的信息(数据属性、函数属性)

# 静态:在定义阶段就确定类型
# 动态:在调用阶段才去确定类型

二:为何要用反射

def func(obj):
    if 'x' not in obj.__dict__:
        return
    obj.x


func(10)        # AttributeError: 'int' object has no attribute '__dict__'

三:实现反射机制的步骤

1.先通过多dir:查看某一个对象下 可以.出哪些属性来

print(dir(obj))     #  ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'age', 'name', 'say']

2.可以通过字符串反射到真正的属性上,得到属性值

print(obj.__dict__['name'])     # xxq
print(obj.__dict__[dir(obj)[-2]])     # xxq

四:四个内置函数的使用:通过字符串来操作属性值

1.hasattr()

print(hasattr(obj, 'name'))     # True
print(hasattr(obj, 'x'))        # False

2.getattr()

print(getattr(obj, 'name'))     # xxq
print(getattr(obj, 'x'))        # AttributeError: 'People' object has no attribute 'x'

3.setattr()

print(getattr(obj, 'name', 'EGON'))     # 修改名字为EGON
print(obj.name)                 # EGON

4.delattr()

delattr(obj, 'name')
print(obj.__dict__)     # {'age': 18}

获取对象和类

res1 = getattr(obj, 'say')      # obj.say
res2 = getattr(People, 'say')      # People.say
print(res1)     # <bound method People.say of <__main__.People object at 0x0167B0B8>>
print(res2)     # <function People.say at 0x016783D0>

查看是否有这个方法

obj = 10
if hasattr(obj, 'x'):
    print(getattr(obj, 'x'))
else:
    print('找不到')      # 找不到

print(getattr(obj, 'x', None))   # None

print(getattr(People, 'say', None))   # <function People.say at 0x01AC83D0>

if hasattr(obj, 'x'):
    setattr(obj, 'x', 1111111)      # 10.x = 1111111
else:
    print('找不到')  # 找不到

基于反射可以十分灵活地操作对象的属性,比如将用户交互的结果反射到具体的功能执行

class Ftp:
    def upload(self):
        print('正在上传')

    def download(self):
        print('正在下载')

    def interactive(self):
        method = input('>>>: ').strip()  # method = 'upload'

        if hasattr(self, method):
            getattr(self, method)()

        else:
            print('该指令不存在!')


obj = Ftp()
obj.interactive()
posted @ 2020-04-15 09:44  轻描丨淡写  阅读(271)  评论(0编辑  收藏  举报