# 反射:
# hasattr() 判断一个对象里是否有对应的 name_str 字符串的方法
# getattr() 根据字符串获得对象里的对应的方法的内存地址 后面+()可调用
# setattr(obj, 'name', value) 通过字符串设置一个值
# delattr(obj, "str")
def eat(self):
print("eat")
class Dog(object):
def __init__(self, name, age):
self.name = name
self.age = age
def talk(self, who):
print("%s is talking wangwangwnag with %s!"% (self.name, who))
d = Dog("L", 11)
action = input("action:>>").strip()
if hasattr(d, action):
func = getattr(d, action)
func("Jy")
print(d.name)
delattr(d, 'name')
else:
setattr(d, action, eat)
func = getattr(d, action)
func(d)
print(d.name)
反射
最新推荐文章于 2025-01-29 09:00:00 发布
1万+

被折叠的 条评论
为什么被折叠?



