码迷,mamicode.com
首页 > 编程语言 > 详细

Python-面向对象之反射

时间:2017-06-10 00:32:39      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:输入   get   反射   log   getattr   color   class   app   main   

反射

  实现了动态的装配,通过字符串来反射类中的属性和方法。

一、hasarttr(obj,name_str)

作用:判断一个对象obj中是否有对应的name_str字符串的属性或者方法。

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

    def eat(self, food):
        print("%s is eating %s" % (self.name, food))


d = Dog("Tom")
d.eat(‘Apple‘)
choice = input(">>>:").strip()

print(hasattr(d, choice))  # obj中是否有对应的choice字符串的属性或者方法

# # 输出
# >>>:name  # 输入对象存在属性
# True
# >>>:eat  # 输入对象存在的方法
# True

二、getattr(obj,name_str)

作用:根据字符串name_str获取obj对象中的对应方法的内存地址或者对应属性的值。

 

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

    def eat(self, food):
        print("%s is eating %s" % (self.name, food))


d = Dog("Tom")
d.eat(‘Apple‘)
choice = input(">>>:").strip()

print(getattr(d, choice))    # 获取d对象中的对应方法的内存地址或者对应属性的值

# >>>:name  # 获取输入对象的值
# Tom
# >>>:eat   # 获取输入对象的方法
# <bound method Dog.eat of <__main__.Dog object at 0x000002A4ADB732E8>>

 

三、setattr(x,y,z)

作用:给obj对象添加一个新属性或者新方法,setattr(x, ‘y‘, v) is equivalent to ``x.y = v‘‘。

1、给对象新增一个新方法

 

def bulk(self):  # 先定义一个bulk函数
    print("%s is yelling..." % self.name)


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

    def eat(self, food):
        print("%s is eating %s" % (self.name, food))


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

setattr(d, choice, bulk)  # 输入的是talk,所以又等同于d.talk = bulk
# d.talk(d) 直接写死,用d.talk(d),一般不这么写
func = getattr(d, choice)  # 用getattr来获取
func(d)

# 输出
# >>>:talk
# Tom is yelling...

 

2、给对象新增一个属性

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

    def eat(self, food):
        print("%s is eating %s" % (self.name, food))


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

setattr(d, choice, ‘Apple‘)  # food,所以又等同于d.food = ‘Apple‘
# print(d.food) 这样就写死了,还是用下面一种
print(getattr(d, choice))

# 输出
# >>>:food
# Apple

四、delattr(x,y)

作用:删除obj对象中的属性或者方法,delattr(x, ‘y‘) is equivalent to ``del x.y‘‘

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

    def eat(self, food):
        print("%s is eating %s" % (self.name, food))


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

delattr(d, choice)  # 根据字符串删除属性或者方法
print(d.name)
print(d.eat)

五、综合使用hasattr、getattr、setattr

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

    def eat(self, food):
        print("%s is eating %s" % (self.name, food))


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

if hasattr(d, choice):  # 判断d对象中存在属性和方法
    name_value = getattr(d, choice)  # 获取属性值
    print(name_value)
    setattr(d, choice, "Jerry")  # 修改属性值
    print(getattr(d, choice))  # 重新获取属性的值
else:
    setattr(d, choice, None)  # 设置不存在的属性值为None
    v = getattr(d, choice)
    print(v)

 

Python-面向对象之反射

标签:输入   get   反射   log   getattr   color   class   app   main   

原文地址:http://www.cnblogs.com/kirusx/p/6974260.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!