zoukankan      html  css  js  c++  java
  • 反射

    一:什么是反射

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

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

    二:为何要用反射

    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'])     # 吴常文
    print(obj.__dict__[dir(obj)[-2]])     # 吴常文

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

    1.hasattr()

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

    2.getattr()

    print(getattr(obj, 'name'))     # 吴常文
    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()
  • 相关阅读:
    在 Spring 中使用 Quartz
    Quartz 快速进阶
    任务调度概述
    Spring Boot 2.x 整合 Mybatis 3.x
    pwd函数实现
    07-图4 哈利·波特的考试 (25 分)
    06-图3 六度空间 (30 分)
    linux中的目录
    Linux中的文件
    06-图2 Saving James Bond
  • 原文地址:https://www.cnblogs.com/2722127842qq-123/p/12717508.html
Copyright © 2011-2022 走看看