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

 

转载于:https://www.cnblogs.com/wuxiaoru/p/11585505.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值