Iron Python 根据字符串输入获取类实例的属性
这是我的代码,我刚学Python,不太明白为什么它不工作。我想在用户选择了某个实例后,能够打印出这个实例的属性。我不确定这是否可能,但如果可以的话,任何帮助都将非常感激。
class Animal(object):
def __init__(self, name, age, happiness, hunger):
self.name = name
self.age = age
self.happiness = happiness
self.hunger = hunger
def animal_print(animal):
if animal.lower() in animals:
print "%s, the %s, is %s years old. % (animal.name, animal, animal.age)
pig = Animal("Joe", 12, 7, 6)
fox = Animal("Fred", 4, 4, 6),
cow = Animal("Bessy", 9, 6, 3),
horse = Animal("Sally", 7, 8, 8),
animals = ["pig", "fox", "cow", "horse"]
animal_print(raw_input("Which animal would you like to see: "))
这段代码的基本思路是,用户会从动物列表中输入一个动物,然后我希望它能返回这个动物的属性。如果我把代码改成下面那样就能工作,但我希望能用一个打印语句来显示所有类实例的属性,而下面的代码需要为每个实例单独写一个打印语句:
def animal_print(animal):
if animal.lower() in animals:
print "%s, the %s, is %s years old % (pig.name, animal, pig.age)
2 个回答
0
你可以考虑用字典来存放你的动物实例。字典的键可以是动物的名字(用字符串表示),而值就是对应的Animal
实例。这样,你的animal_print()
函数就只需要负责打印动物的属性了。
def animal_print(animal):
print "%s, the %s, is %s years old." % (animal.name, animal, animal.age)
animals = {
'pig': Animal("Joe", 12, 7, 6),
'fox': Animal("Fred", 4, 4, 6),
'cow': Animal("Bessy", 9, 6, 3),
'horse': Animal("Sally", 7, 8, 8),
}
user_animal = raw_input("Which animal would you like to see: ")
try:
animal_print(animals[user_animal.lower()])
except KeyError:
print "I no nothing about animal %s." % user_animal
0
字符串 'pig'
和存储在变量 pig
中的 Animal
实例是不同的东西。
raw_input
返回的是一个字符串 str
。所以在 animal_print
函数里,animal
实际上是一个字符串。这个字符串没有 name
这个属性,所以当你尝试使用 animal.name
时,会出现一个 AttributeError
错误。
有至少三种方法可以修复这个代码:使用一个“白名单”字典,从 globals()
字典中查找值,或者使用 eval
。在这三种方法中,使用白名单是最安全的,因为全局查找和 eval
不应该用于任意用户输入。这样做可能会泄露私人信息,而后者则可能让恶意用户执行任意代码。
所以,建议使用白名单:
animap = {'pig': pig,
'fox': fox}
anistr = raw_input(...) # str
animal = animap[anistr] # Animal instance
class Animal(object):
def __init__(self, name, age, happiness, hunger):
self.name = name
self.age = age
self.happiness = happiness
self.hunger = hunger
def animal_print(anistr):
animal = animap.get(anistr.lower())
if animal is not None:
print "%s, the %s, is %s years old." % (animal.name, anistr, animal.age)
pig = Animal("Joe", 12, 7, 6)
fox = Animal("Fred", 4, 4, 6)
cow = Animal("Bessy", 9, 6, 3)
horse = Animal("Sally", 7, 8, 8)
animap = {'pig': pig,
'fox': fox,
'cow': cow,
'horse': horse}
anistr = raw_input("Which animal would you like to see: ")
animal_print(anistr)