Python类(五)-反射

反射即通过字符串映射或修改程序运行时的状态、属性、方法

有4个方法:

hasattr():

hasattr(object,string):object为实例化的对象,string为字符串

判断对象object里是否有对应字符串string的方法或属性,返回布尔型

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

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

    def eat(self):
        print('%s is eating '%self.name)

if __name__ == '__main__':
    p = Person('John') #实例化Person类
    argu = input(">>>:").strip()
    print(hasattr(p,argu))

 运行,输入name

类中有name这个属性,返回True

输入eat

类中有eat这个方法,返回True

输入test

类中没有test这个属性和方法,返回False

getattr():

getattr(object,string[,default]):object为实例化的对象,string为字符串,default为默认值

根据字符串string来获取对象object中的方法或属性,如果对象object中没有字符串string的属性或方法,就会返回default的值,如果不设置default的值,将会报错

获取类中方法并调用

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

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

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

if __name__ == '__main__':
    p = Person('John')
    argu = input(">>>:").strip()
    if hasattr(p,argu):
        getattr(p,argu)('meat')

 执行结果

获取类中属性的值

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

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

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

if __name__ == '__main__':
    p = Person('John')
    argu = input(">>>:").strip()
    print(getattr(p,argu))

如果字符串不是类中的属性,则返回规定的默认值

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

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

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

if __name__ == '__main__':
    p = Person('John')
    argu = input(">>>:").strip()
    print(getattr(p,argu,"test"))

 输入a,类中没有a这个属性,则返回规定的默认值test

setattr():

setattr(object,string,value):object为实例化的对象,string为字符串,value为值

setattr()用来设置方法或属性

把传入的字符串string设置为value名的函数,再通过getattr()来调用

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

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

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

#在类外定义一个函数
def talk(self):
    print("%s is talking"%self.name)

if __name__ == '__main__':
    p = Person('John')
    argu = input(">>>:").strip()
    setattr(p,argu,talk) #把传入的字符串设置为talk函数
    getattr(p,argu)(p) #把实例化的对象传入函数中

 运行,输入a

调用的不是talk()函数,而是设置为talk()函数的字符串a

setattr()设置属性

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

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

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

if __name__ == '__main__':
    p = Person('John')
    argu = input(">>>:").strip()
    setattr(p,argu,'Man') #设置属性
    print(getattr(p,argu))

传入的字符串string,如果不是类中的属性,将给类创建新属性string,并赋值value给新属性

传入的字符串string,如果是类中的属性,value将覆盖原有的属性

delattr():

delattr(object,string):删除object对象中的string属性

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

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

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

if __name__ == '__main__':
    p = Person('John')
    argu = input(">>>:").strip()
    delattr(p,argu)
    print(getattr(p,argu))

 运行,输入name

报错,Person类中没有了name属性

posted @ 2018-01-27 22:36  Sch01aR#  阅读(193)  评论(0编辑  收藏  举报