zoukankan      html  css  js  c++  java
  • 【python基础】反射方法

      1、getattr()

        功能:获取实例的属性值或者方法地址
        返回值:属性的值/方法的地址

        getattr(对象,属性名/方法名)
        getattr(类,属性名/方法名)

     1 class Person:
     2     country = "中国"
     3 
     4     def __init__(self, name, age):
     5         self.name = name
     6         self.age = age
     7 
     8     def show_info(self):
     9         print("我的姓名: %s, 年龄: %d" % (self.name, self.age))
    10 
    11     @classmethod
    12     def show_country(cls):
    13         print(cls.country)
    14 
    15 
    16 p1 = Person("xixi", 7)
    17 
    18 name = getattr(p1, "name")
    19 print(name)  # xixi
    20 
    21 show_info = getattr(p1, "show_info")
    22 print(show_info)  # <bound method Person.show_info of <__main__.Person object at 0x000000000294AD68>>
    23 
    24 show_info()  # 方法调用   我的姓名: xixi, 年龄: 7
    25 
    26 print(getattr(Person, "country"))  # 中国
    27 
    28 show_country = getattr(Person, "show_country")
    29 print(show_country)  # <bound method Person.show_country of <class '__main__.Person'>>
    30 show_country()  # 中国

      2、hasattr()

        功能:检测是否具有某个类方法,类属性,实例属性,实例方法
        返回值:bool值

        hasattr(对象,实例属性/实例方法)
        hasattr(类,类属性/类方法)

     1 class Person:
     2     country = "中国"
     3 
     4     def __init__(self, name, age):
     5         self.name = name
     6         self.age = age
     7 
     8     def show_info(self):
     9         print("我的姓名: %s, 年龄: %d" % (self.name, self.age))
    10 
    11     @classmethod
    12     def show_country(cls):
    13         print(cls.country)
    14 
    15 
    16 p1 = Person("xixi", 200)
    17 
    18 print(hasattr(p1, "name"))  # True
    19 print(hasattr(p1, "show_info"))  # True
    20 print(hasattr(p1, "get_num"))  # False
    21 
    22 print(hasattr(Person, "country"))  # True
    23 print(hasattr(Person, "show_country"))  # True

      3、setattr()

        功能: 设置实例/类的属性值
        返回值: 无

        setattr(对象, 属性名, 属性值)
        setattr(类, 属性名, 属性值)

        如果属性已存在, 重新赋值;
        如果属性不存在, 添加属性并赋值;

     1 class Person:
     2     country = "中国"
     3 
     4     def __init__(self, name, age):
     5         self.name = name
     6         self.age = age
     7 
     8     def show_info(self):
     9         print("我的姓名: %s, 年龄: %d" % (self.name, self.age))
    10 
    11     @classmethod
    12     def show_country(cls):
    13         print(cls.country)
    14 
    15 
    16 p1 = Person("xixi", 6)
    17 
    18 setattr(p1, "age", 7)
    19 print(p1.age)  # 7
    20 
    21 setattr(p1, "friend", "haha")
    22 print(p1.friend)  # haha
    23 
    24 setattr(Person, "country", "中华人民共和国")
    25 print(Person.country)  # 中华人民共和国
    26 
    27 setattr(Person, "color", "")
    28 print(Person.color)  #
    29 print(p1.color)  #

      4、delattr()

        功能: 删除对象/类的属性  
        返回值: 无

        delattr(对象, 属性名)
        delattr(类, 属性名)

        要删除的属性不存在, 报错AttributeError: color

     1 class Person:
     2     country = "中国"
     3 
     4     def __init__(self, name, age):
     5         self.name = name
     6         self.age = age
     7 
     8     def show_info(self):
     9         print("我的姓名: %s, 年龄: %d" % (self.name, self.age))
    10 
    11     @classmethod
    12     def show_country(cls):
    13         print(cls.country)
    14 
    15 
    16 p1 = Person("xixi", 6)
    17 
    18 delattr(p1, "age")
    19 # print(p1.age)  # AttributeError: 'Person' object has no attribute 'age'
    20 
    21 delattr(Person, "country")
    22 # print(Person.country)  # AttributeError: type object 'Person' has no attribute 'country'
    23 # print(p1.country)  # AttributeError: 'Person' object has no attribute 'country'
    24 
    25 # delattr(p1, "color")  # AttributeError: color
  • 相关阅读:
    maven中如何打包源代码
    多模块Maven项目如何使用javadoc插件生成文档
    maven 添加jar到中央/远程仓库
    maven中的snapshot来源与注意事项
    maven 管理
    spring 判断非空提示断言
    powerdesigner 生成数据库脚本
    GOOD BLOG URL
    正则表达式 匹配中文,英文字母和数字及_的写法!同时控制长度
    oracle 学习blogs
  • 原文地址:https://www.cnblogs.com/Tree0108/p/12112961.html
Copyright © 2011-2022 走看看