• 反射


    getattr,hasattr:  执行,判断

    ####  反射
    # 可以简化代码,操作更加简洁
    # 用字符串数据类型的变量名来访问这个变量的值
    
    
    # # 类 ,静态属性,类方法,静态方法
    # 命名空间.xxx 都可以getattr(命名空间,"xxx")
    # class Student:
    #     def __init__(self):pass
    #     def check_course(self):
    #         print("check_course")
    #     def choose_course(self):
    #         print("choose_course")
    #
    #     def choosed_course(self):
    #         print("查看已经选择的课程")
    # # 如果创建一个对象,对对象操作多次,会让代码很冗余
    
    class Student:
        name = "stu"
        @classmethod
        def check_course(cls):
            print("查看课程")
        @staticmethod
        def login():
            print("登录")
    
    # s1 = Student()
    # print(Student.name)
    # 反射查看静态属性
    # print(getattr(Student,"name"))
    # print(getattr(s1,"name")) # stu
    
    # 反射调用方法:
    # getattr(s1,"login")()
    # getattr(s1,"check_course")()
    # getattr(Student,"check_course")()
    # # 登录
    # # 查看课程
    # # 查看课程
    
    # num = input(">>>")
    # getattr(Student,num)()
    
    # num = input(">>>")
    # print(hasattr(Student,num))  # True  判断类和对象中是否有该函数,有则返回True
    # num = input(">>>")
    # print(hasattr(s1,num))
    # num = input(">>>")
    # if hasattr(s1,num):
    #     getattr(s1,num)()
    # else:
    #     print("报错")
    
    # # 对象
    # class A:
    #     def __init__(self,name):
    #         self.name = name
    #
    #     def func(self):
    #         return "in func"
    #
    # a = A("aaa")
    # print(a.name)
    # print(getattr(a,"name"))
    # print(getattr(a,"func")())
    
    # # 模块
    # import os
    # # os.rename("__init__.py","init")
    # getattr(os,"rename")("init","__init__.py")
    # # getattr(os,"rename")  相当于 os.rename  任何一个函数不加()都相当于内存地址
    # def ha():
    #     print("ha")
    # def he():
    #     print("he")
    # import sys
    # my_file = sys.modules["__main__"]
    # getattr(my_file,"he")()
    # getattr(my_file,"ha")()
    
    # 反射模块中的内容
    # # def ha():
    # #     print("ha")
    # # def he():
    # #     print("he")
    # # import sys
    # # my_file = sys.modules["__main__"]
    # # getattr(my_file,"he")()
    # #getattr(my_file,"ha")()
    
    
    # 反射
    # hasattr, getattr
    # 类名.名字
         # getattr(类名,"名字")
    # 模块名.名字
        # import 模块
        # getattr(模块,"名字")
    # 自己文件.名字
        # import sys
        # getattr(sys.modules["__main__"],"名字")

    delattr,setattr  删除,修改

    class A:
        def __init__(self,name):
            self.name = name
    
    a = A("dudu")
    print(a.name)
    # a.name = "ssss"
    # # print(a.name)
    
    setattr(a,"name","abcd")   # o,name,value   字符串形式,修改内容,很少使用
    print(a.name)
    # del a.name
    # print(a.__dict__)
    
    delattr(a,"name")  # o,name:str
    print(a.__dict__)   删除对象属性
  • 相关阅读:
    xposed的基本使用
    scrapy发送邮件
    Image Pipeline
    javax.validation.ConstraintViolationException---Hibernate后台实体校验
    oracle中lead和lag函数 (转载)
    oracle中根据当前记录查询前一条和后一条记录
    物化视图知识了解(转载)
    10个线程同步处理1000行消息
    SQL语句执行效率及分析(note)
    js对文本框特殊字符串过滤
  • 原文地址:https://www.cnblogs.com/yfjly/p/10598597.html
Copyright © 2020-2023  润新知