标签:pre 林志玲 set xxxxx 没有 ast 大牛 setattr print
import master # 报错不用管
print("""
1. chi: 大牛特别能吃
2. he: 大牛特别能喝
3. shui: 大牛特别能睡
4. play: 大牛特别能玩儿
5. sa: 大牛很喜欢撒谎
""")
while 1:
content = input("请输入你要执行的函数:")
if content == "1":
master.chi()
elif content == "2":
master.he()
elif content =="3":
master.shui()
elif content == "4":
master.play_1()
elif content == "5":
master.sa()
while 1:
content = input("请输入你要测试的功能:") # 用户输入的功能是一个字符串
# 判断 xxx对象中是否包含了xxxxx
if hasattr(master, content):
xx = getattr(master, content)
xx()
print("有这个功能")
else:
print(‘没有这个功能‘)
# 对于模块而言可以使用getattr, hasattr, 同样对于我们的对象也可以执行类似的操作
class Person:
def __init__(self, name, laopo):
self.name = name
self.laopo = laopo
p = Person("宝宝", "林志玲")
print(hasattr(p, "laopo")) #
print(getattr(p, "laopo")) # p.laopo
setattr(p, "laopo", "胡一菲") # p.laopo = 胡一菲
setattr(p, "money", 100000000000) # p.money = 100000000
print(p.laopo)
print(p.money)
delattr(p, "laopo") # 把对象中的xxx属性移除. != p.laopo = None
print(p.laopo)
标签:pre 林志玲 set xxxxx 没有 ast 大牛 setattr print
原文地址:https://www.cnblogs.com/demons97/p/10187529.html