Я не могу создать новые экземпляры класса Pokemon
, используя элемент списка line[1]
в качестве имени экземпляра.
pokedex = open('../resource/lib/public/pokedex.csv', 'r')
first_line = pokedex.readline() #Skip the header
class Pokemon:
def __init__(self, number, name, type1, type2, HP, attack, defense, special_atk,special_def, speed, generation, legendary, mega):
self.number = int(number)
self.name = str(name)
self.type1 = type1
self.type2 = type2
self.HP = int(HP)
self.attack = int(attack)
self.defense = int(defense)
self.special_atk = int(special_atk)
self.special_def = int(special_def)
self.speed = int(speed)
self.generation = int(generation)
self.legendary = bool(legendary)
self.mega = bool(mega)
for line in pokedex:
line = line.strip().split(",")
#line[1] is the name (string) of the Pokemon instance
line[1] = Pokemon(*line)
print(Kakuna) #NameError: name 'Kakuna' is not defined
print(line[1]) #This gives a correct instance created from the last line of the file