[英]Creating multiple class instances from a list of names in Python?
Can someone explain to me how to return a new instance of a class by iterating over a list of names and a list of dicts? 有人可以通过迭代一个名单列表和一个dicts列表向我解释如何返回一个新的类实例?
I know that I can unpack my dictionary using **
, and I know that I can iterate through my list of dictionaries. 我知道我可以使用**
解压缩字典,我知道我可以遍历我的字典列表。
So I attempted to create a for
loop function that creates new instances of my Card
class for each card in the cardList
. 所以我试图创建一个for
循环函数,为cardList
每张卡创建我的Card
类的新实例。
Here is the code I thought would work: 这是我认为可行的代码:
def createCardInstances(x):
for i in range(len(x)):
namesOfCards[i] = Card(**cardList[i])
return namesOfCards[i]
namesOfCards
is a list of card names that I would like to turn into instances of the Card
class. namesOfCards
是一个卡片名称列表,我想将其转换为Card
类的实例。 cardList
is a list of the dictionaries that represent cards and their values. cardList
是表示卡片及其值的字典列表。
For instance, I can do this: 例如,我可以这样做:
Knight = Card(**cardList[0])
Which works fine. 哪个工作正常。 But, if I'm developing a card game and have upwards of a 100 individual cards or more, I would like to avoid having to write out each card name individually and copy\\pasting the =Card(**cardList[])
code each time. 但是,如果我正在开发一款纸牌游戏并且拥有超过100张个人卡或更多卡,我希望避免单独写出每个卡名并复制\\粘贴=Card(**cardList[])
代码时间。
How do I automate the creation of new instances? 如何自动创建新实例? Is that even feasible? 这甚至可行吗?
EDIT: 编辑:
I'm trying to use the names in namesOfCards
to make new instances of Card
. 我正在尝试使用namesOfCards
的名称来创建Card
新实例。 It was suggested to me that my next step in learning python while creating a card game was to make it more object oriented. 有人向我建议,我在创建卡片游戏时学习python的下一步是让它更加面向对象。
So my goal right now is to use the names of the cards - saved in the list namesOfCards
- to make new instances of Card
. 所以我现在的目标是使用卡片的名称 - 保存在列表namesOfCards
- 来创建Card
新实例。 ie: 即:
[Knight, Mage, Warrior, ...]
And I want to have the ability to take those and do this: 我希望有能力接受这些并做到这一点:
Knight = Card(**cardList[0])
Mage = Card(**cardList[1])
Warrior = Card(**cardList[2]
and so on and so forth. 等等等等。
Is that possible? 那可能吗? Or do I need to use the list comprehension suggestion to store all of the class instances into a list, and then have to access the instances by using new_cards[0].name
or new_cards[1].attack
? 或者我是否需要使用列表new_cards[0].name
建议将所有类实例存储到列表中,然后必须使用new_cards[0].name
或new_cards[1].attack
来访问实例?
you are returning an individual card when you pass it a list? 当你把名单传递给你时,你会退还一张卡片吗? also if cardNames
is a list of names you shouldn't be overwriting it with Card
objects. 如果cardNames
是一个名字列表,你不应该用Card
对象覆盖它。 You should be able to do something like this? 你应该能够做这样的事情吗?
new_cards = [Card(**card) for card in card_list] # make new card objects
cards = {card.name: card for card in new_cards} # dict to ref cards by name
print cards['Knight'].name
or simply 或者干脆
cards = {card['name']: Card(**card) for card in card_list}
if you are wanting to restrict that cards to only those in namesOfCards
. 如果你想将这些卡限制在namesOfCards
。 you could 你可以
cards = {card['name']: Card(**card) for card in card_list if card['name'] in names_of_cards}
( note : this all assumes that you dont have two cards with the same name) ( 注意 :这都假设你没有两张同名的牌)
It is also possible to put those cards into your local namespace like the question asks, but I would highly discourage it. 也可以像问题一样将这些卡放入你的本地命名空间,但我会高度反对它。 But if you really really want to ... 但如果你真的想......
locals().update(cards)
print Knight.name
make sure that your list contains string like 确保您的列表包含字符串
CARDS = ['Knght', 'Mage'] #and so on
I'm not sure if this is what you need but see the code below: 我不确定这是否是你需要的,但请看下面的代码:
CARD = ['knight','mage']
class cards:
def __init__(self, name):
self.n = name
self.attack = "attack " + self.n
print knight.attack
using that you can loop through a list or dictionary to create an instance of your class 使用它,您可以遍历列表或字典来创建类的实例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.