• python学习第七周之反射


    1.反射:通过字符串映射或修改程序运行时的状态。

    有四个方法:hasattr() getattr() setattr() delattr()

    2.(1)如以下程序:

    hasattr() 判断有没有属性 ,hasattr(d,choice)分别为 对象名 字符串

    getattr() 调用该属性

    class Dog(object):
        def __init__(self,name):
            self.name=name
        def eat(self):
            print("%s is eating " % self.name)
    d=Dog("chen")     #实例化一个狗
    choice=input(">>:").strip()
    
    print(hasattr(d,choice)) #判断有没有 对象名,字符串形式
    print(getattr(d,choice))  #映射出内存对象地址
    getattr(d,choice)()    #调用
    

     输出结果为:

    >>:eat
    True
    <bound method Dog.eat of <__main__.Dog object at 0x00CEE030>>
    chen is eating 
    

     (2)可以将其写入for循环,如果没有,就添加方法

    class Dog(object):
        def __init__(self,name):
            self.name=name
    
        def eat(self,food):
            print("%s is eating " % self.name,food)
    
    d=Dog("chen")     #实例化一个狗
    choice=input(">>:").strip()
    
    if hasattr(d,choice):
        func=getattr(d,choice)
        func("rice")
    

     (3)动态装配一个方法:动态的将类外面的方法装配到类里面

    如果我们要添加新的属性,就要用到 setattr()方法

    在类外面定义一个方法bulk,使用setattr添加一个新的方法

    def bulk(self):
        print("%s is yelling..." %self.name)
    class Dog(object):
        def __init__(self,name):
            self.name=name
        def eat(self,food):
            print("%s is eating " % self.name,food)
    d=Dog("chen")     #实例化一个狗
    choice=input(">>:").strip()
    if hasattr(d,choice): func=getattr(d,choice) func("rice") else: setattr(d,choice,bulk) d.talk(d) #动态装饰一个方法,bulk中没有写self,需要将自己传进去;动态的将类外面的方法装配到类里面;bulk相当于是一个变量名

     输出结果为:输入talk,输出chen is yelling...

     (4)动态装配一个属性

    def bulk(self):
        print("%s is yelling..." %self.name)
    
    class Dog(object):
        def __init__(self,name):
            self.name=name
        def eat(self,food):
            print("%s is eating " % self.name,food)
    d=Dog("chen")     #实例化一个狗
    choice=input(">>:").strip()
    
    if hasattr(d,choice):
        func=getattr(d,choice)
        func("rice")
    else:
        setattr(d,choice,22)   #setattr(d,choice,None)    #动态装饰一个属性
        print(getattr(d,choice))
    

     这时输出结果为:将22赋给了输入的属性22;如果输入money,则将22赋给money

    >>:age
    22
    

     (5)修改名字使用setattr

    def bulk(self):
        print("%s is yelling..." %self.name)
    
    class Dog(object):
        def __init__(self,name):
            self.name=name
        def eat(self,food):
            print("%s is eating " % self.name,food)
    d=Dog("chen")     #实例化一个狗
    choice=input(">>:").strip()
    
    if hasattr(d,choice):
        setattr(d,choice,"wang")   #修改,setattr给对象添加一个新的属性;修改name
    else:
        setattr(d,choice,22)   #setattr(d,choice,None)    #动态装饰一个属性
        print(getattr(d,choice))
    print(d.name)
    

     这时输出结果为:

    >>:name
    wang
    

     (6)删除

    if hasattr(d,choice):
        delattr(d,choice)    #删除
    else:
        setattr(d,choice,22)   #setattr(d,choice,None)    #动态装饰一个属性
        print(getattr(d,choice))
    print(d.name)
    

     输入name后,执行报错。

    总结:
        hasattr(obj,name_str) , 判断一个对象obj里是否有对应的name_str字符串的方法
        getattr(obj,name_str), 根据字符串去获取obj对象里的对应的方法的内存地址
        setattr(obj,'y',z), 相当于obj.y = v
        delattr

  • 相关阅读:
    学习笔记180—回归系数与相关系数的关系和区别
    学习笔记178—精品书籍推荐榜
    学习笔记177—PPT生成的图片设置成特定像素级的图片【四种方法】
    学习笔记176—PS 获得一个椭圆的某个部分
    shell:利用sed删除文件中的匹配行
    常用壁纸
    Linux下编译C文件:Hello World
    属性访问、特性和修饰符
    介绍Python基本特殊方法
    kafka配置
  • 原文地址:https://www.cnblogs.com/wuxiaoru/p/11585505.html
Copyright © 2020-2023  润新知