Python反射实例解析

在Python中,反射的作用是很重要的,而它的作用就是通过字符串映射或修改程序运行时的状态、属性、方法。

具体的有以下四种

1、getattr

    getattr(object, name, default=None)    # 获取一个对象里对应字符串的方法
def getattr(object, name, default=None): # known special case of getattr
    """
    getattr(object, name[, default]) -> value

    Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.
    When a default argument is given, it is returned when the attribute doesn't
    exist; without it, an exception is raised in that case.
    """
    pass

2、hasattr

hasattr(obj,name_str)  # 判断obj中有没有一个对应name_str字符串对应的方法或属性
def hasattr(*args, **kwargs): # real signature unknown
    """
    Return whether the object has an attribute with the given name.
    
    This is done by calling getattr(obj, name) and catching AttributeError.
    """
    pass

3、setattr

setattr(x, y, v)    # 设置属性或方法
def setattr(x, y, v): # real signature unknown; restored from __doc__
    """
    Sets the named attribute on the given object to the specified value.
    
    setattr(x, 'y', v) is equivalent to ``x.y = v''
    """
    pass

4、delattr

delattr(x, y)    # 删除属性或方法
def delattr(x, y): # real signature unknown; restored from __doc__
    """
    Deletes the named attribute from the given object.
    
    delattr(x, 'y') is equivalent to ``del x.y''
    """
    pass

 

分别以hasattr、getattr、setattr、delattr举例:

首先,hasattr:

class Foo(object):
    def __init__(self,name):
        self.name = name


    def test(self):
        print("%s is testing..."%self.name)

d = Foo("Tom")
choice = input(">>>>:").strip()
# d.choice  行不通,choice是字符串

print(hasattr(d,choice))    # 判断eat方法是否存在

然后,getattr:

class Foo(object):
    def __init__(self,name):
        self.name = name

    def test(self):
        print("%s is test......"%self.name)

d = Foo("Tom")
choice = input(">>>>:").strip()
print(getattr(d,choice))    # 打印eat方法在内存中的对象地址
c = getattr(d,choice)
c()

紧接着,setattr:

# 第一种,设置方法
def bulk(self):
    print("%s is d...."%self.name)    # 新加方法

class Foo(object):
    def __init__(self,name):
        self.name = name


    def test(self):
        print("%s is test......"%self.name)

d = Foo("Tom")
choice = input(">>>>:").strip()
if hasattr(d,choice):
    f = getattr(d,choice)
    f()
else:
    setattr(d,choice,bulk)      # 动态向class里面装入了一个方法
    d.bulk(d)           
# 这里的b.bluk仅仅是个变量名,随时可以替代,
# 如写成d.talk(d),那么也是没有问题的,不过后面调用的时候choice的input就要变成talk方法
# 第二种,设置(修改)属性
def bulk(self):
    print("%s is d...."%self.name)    # 新加方法

class Foo(object):
    def __init__(self,name):
        self.name = name


    def test(self):
        print("%s is testing..."%self.name)

d = Foo("Tom")
choice = input(">>>>:").strip()
if hasattr(d,choice):
    setattr(d,choice,'Dom')     # 将已有的str进行修改了,待打印name的时候,name就变了   
# 动态传入属性
else:
    setattr(d,choice,None)      # 动态传入属性
    print(getattr(d,choice))
print(d.name) 

最后,delattr:

class Foo(object):
    def __init__(self,name):
        self.name = name


    def test(self):
        print("%s is test......"%self.name)

d = Foo("Tom")
choice = input(">>>>:").strip()

if hasattr(d,choice):
    delattr(d,choice)

print(d.name)

 

总结:

# 反射:
# hasattr(obj,name_str),判断一个对象obj里是否有对应的name_str字符串的方法
# getattr(obj,name_str),根据字符串去获取obj对象里的对应的方法的内存地址
# setattr(obj,'y',z),通过字符串设定属性,is equivalent to ``x.y = v'',给对象添加新属性
# delattr(obj,name_str),删除

实际上就是把字符串反射回内存中的地址,来回映射,实现动态内存装配。

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jeromeyoung666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值