zoukankan      html  css  js  c++  java
  • 反射

    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__)   删除对象属性
  • 相关阅读:
    sql分页查询
    SQL语句优化技术分析
    大型数据库的设计原则与开发技巧
    Microsoft SharePoint Server 2010 的新增功能
    Installing SharePoint 2010 on Windows 7
    Missing the ManageContent and structure in MOSS 2010
    Simple SharePoint 2010 + Silverlight + Client Object Model Example
    SharePoint 2010 Central AdminCreate/Extend Web Application button on Ribbon are disabled
    利用SharePoint Designer 修改列表页面实例
    数据库设计中的14个技巧
  • 原文地址:https://www.cnblogs.com/yfjly/p/10598597.html
Copyright © 2011-2022 走看看