Python方法不在obj中

2024-06-16 12:29:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为一个对象编写一个方法,我不断得到以下错误:'MagicCardSet'对象没有属性'get\u\u'colors'。我所有的其他方法工作除了这个所以我有点困惑!谢谢!你知道吗

创建魔术卡集的对象:

from collections import Counter

class MagicCardSet(object):


    def __init__(self, DeckDict):
        self.cardlist = [MagicCard(eachCard) for eachCard in DeckDict["cards"]]


    def get_card_list(self):
        Card_name_list=[]
        for newCard in self.cardlist:
            Card_name_list.append(newCard.get_name())
        return(Card_name_list)


    def get_card_color(self):
        color_list=[]
        for newCard in self.cardlist:
            color_list.append(newCard.get_colors())
        return(color_list)   


    def get_card_rarity(self):
        rarity_list=[]
        for newCard in self.cardlist:
            rarity_list.append(newCard.get_rarity())
        return(rarity_list)


    def get_rarest_card(self):

        card_rarities = Counter(card.rarity for card in self.cardlist if card.rarity !="Basic Land")
        rarity = min(card_rarities, key=lambda card: card_rarities[card])  # Effectively an argmin
        card_names=[card.get_name() for card in self.cardlist if card.rarity == rarity]
        card_colors=[card.get_colors() for card in self.cardlist if card.rarity == rarity]
        return (list(zip(card_names,card_colors)))


    def get_uncommon_colors(self):
        uncommon_list=[]
        for newCard in self.cardlist:
            if newCard.get_rarity()=="Uncommon":
                uncommon_list.append(newCard.get_colors())
        return(uncommon_list)

电话:

DTK_Set=MagicCardSet(DTK_information)

DTK_Set.get_uncommon_colors()

打开和存储JSON文件

import json
with open('DTK.json',encoding="utf-8") as data_file:
    DTK_information = json.loads(data_file.read())

JSON文件中的内容:

{
    "name" : "Sen Triplets",
    "manaCost" : "{2}{W}{U}{B}",
    "cmc" : 5,
    "colors" : ["White", "Blue", "Black"],

    "type" : "Legendary Artifact Creature — Human Wizard",
    "supertypes" : ["Legendary"],
    "types" : ["Artifact", "Creature"],
    "subtypes" : ["Human", "Wizard"],

    "rarity" : "Mythic Rare",
}

Trace Error (Hopefully this helps a little more):

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-52-2c1a44dc0874> in <module>()
----> 1 DTK_Set.get_uncommon_colors()

AttributeError: 'MagicCardSet' object has no attribute 'get_uncommon_colors'

Tags: nameinselfforgetreturndefcard