I have the following class setup:
class Card(object):
def __init__(self, name="", attack=0, defense=0, magic=0, shield=0, description=""):
self.name = name
self.attack = int(attack)
self.defense = int(defense)
self.magic = int(magic)
self.shield = int(shield)
self.description = description
I would like to make instances of Card using a list of dictionaries created from csv.dictreader.
Here is what the method for determining my cardList returns:
[
{'Magic': '200', 'Shield': '100', 'NameOfCard': 'Knight', 'Attack': '700', 'Defense': '400', 'Description': ''},
{'Magic': '500', 'Shield': '500', 'NameOfCard': 'Mage', 'Attack': '0', 'Defense': '0', 'Description': ''},
{'Magic': '100', 'Shield': '100', 'NameOfCard': 'Peasant', 'Attack': '100', 'Defense': '100', 'Description': ''},
{'Magic': '0', 'Shield': '0', 'NameOfCard': 'Lancer', 'Attack': '400', 'Defense': '100', 'Description': ''},
{'Magic': '100', 'Shield': '200', 'NameOfCard': 'Guardian', 'Attack': '100', 'Defense': '600', 'Description': ''},
...]
I was hoping to be able to use the 'NameOfCard' values to name the instances, and then map the values to the arguments taken by the __init__ method in the Card class.
My first thought was to do something like this:
Knight = Card(cardList[0]('NameOfCard')...)
But calling print Knight.name returns TypeError: 'dict' object is not callable.
How do I use my list of dicts to create instances of the Card class?