zoukankan      html  css  js  c++  java
  • 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()
    
  • 相关阅读:
    使用Maven Helper解决Maven依赖冲突
    bzoj 1037[ZJOI2008]生日聚会Party
    bzoj 1031[JSOI2007]字符加密
    bzoj 1029 [JSOI2007]建筑抢修
    bzoj 1025[SCOI2009]游戏
    bzoj 1027[JSOI2007]合金
    bzoj 1024[SCOI2009]生日快乐
    bzoj 1023[SHOI2008]cactus仙人掌图
    bzoj 1022 [SHOI2008]小约翰的游戏John
    bzoj 1021[SHOI2008]Debt 循环的债务
  • 原文地址:https://www.cnblogs.com/xuexianqi/p/12703295.html
Copyright © 2011-2022 走看看