在最后一行中,我得到了我的对象ant003未定义的错误。但为什么呢?
class Ant:
def __init__(self, name):
"""Initializes the data."""
self.name = name
self.food = 0
self.posx = 0
self.posy = 0
print("(Initializing {0})".format(self.name))
def searchFood(self):
self.food = self.food + 1
print("Ant {1} has {0} food.".format(self.food, self.name))
ant001 = Ant("Anton")
ant001.searchFood()
ant002 = Ant("Bob")
ants = {'ant003': None, 'ant004': None}
for name in ants.keys():
ants[name] = Ant(name)
print ants[name]
#print ant001
#print ant003
ant003.searchFood() # NameError: name 'ant003' is not defined