类中的反射(查找)
类中的反射(查找)
getattr(object,name,default=None) #获取object里面对应对象的内存地址
hasattr(a,b) #如果对象a中有变量b,返回true
setattr(x,y,z) #设置一个新方法
delattr(x,y)
def bulk(self)
print("%s is yelling...."%self.name)
class Dog(object)
def __inint__(self,name);
self.name=name
def eat(self,food)
print("%s is eating???"%self.name,food)
d = Dog("NiuHanYang")
choice = input(">>:").strip()
#判断对象里面有没有choice这个方法,如果有eat就执行eat属性,如果没有就创建一个choice对应的bulk属性
#setattr(object对象, name字符串, value属性值)
if hasattr(d,choice)
func = getattr(d,choice)
func()
else
setattr(d,choice,bulk)
fun = getarrt(d,choice)
fun(d)