Python AttributeError:类型对象“x”没有属性“x”

2024-05-15 01:58:14 发布

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

我目前正在用Python开发一个简单的基于文本的游戏,只是为了练习Python和面向对象编程,但是我遇到了一个错误,它告诉我“LargeManaPotion”没有属性“name”,当我看到它的时候,它的声明方式与“SmallManaPotion”完全相同,效果很好。我想这是一个愚蠢的错误,我只是忽视了或其他什么,但会感谢帮助。另外,当我在播放器.库存函数,所以我不知道为什么它不能在交易函数中工作。不管怎样,这是相关代码。提前谢谢。在

class ManaPotion:
    def __init__(self):
        raise NotImplementedError("Do not create raw ManaPotion objects.")

    def __str__(self):
        return "{} (+{} Mana)".format(self.name, self.mana_value)


class LargeManaPotion(ManaPotion):
    def __init__(self):
        self.name = "Large Mana Potion"
        self.mana_value = 45
        self.value = 40


class SmallManaPotion(ManaPotion):
    def __init__(self):
        self.name = "Small Mana Potion"
        self.mana_value = 15
        self.value = 10

正如你所看到的,它和小马拿药水是一样的。 这是导致错误的函数。在

^{pr2}$

但是,此函数调用LargeManaPotion,但没有任何错误。在

def print_inventory(self):
    print("Inventory:")
    for item in self.inventory:
        print('* ' + str(item))
    print("* Gold: {}".format(self.gold))
    best_weapon = self.most_powerful_weapon()
    print("Your best weapon is your {}".format(best_weapon))

错误和堆栈跟踪:

Choose an action: 
i: Print inventory
t: Trade
n: Go north
s: Go south
w: Go west
m: Replenish Mana
Action: t


Gold: 33 
Would you like to (B)uy, (S)ell, or (Q)uit?
>>>b

Gold: 33 
Here's whats available to buy: 
1. Crusty Bread - 12 Gold
2. Crusty Bread - 12 Gold
3. Crusty Bread - 12 Gold
4. Healing Potion - 60 Gold
5. Healing Potion - 60 Gold
6. Small Mana Potion - 10 Gold
7. Small Mana Potion - 10 Gold

Traceback (most recent call last):

File "/Users/Cpt_Chirp/Documents/Escape/game.py", line 74, in <module>
play()

File "/Users/Cpt_Chirp/Documents/Escape/game.py", line 17, in play
choose_action(room, player)

File "/Users/Cpt_Chirp/Documents/Escape/game.py", line 30, in choose_action
action()

File "/Users/Cpt_Chirp/Documents/Escape/player.py", line 112, in trade
room.check_if_trade(self)

File "/Users/Cpt_Chirp/Documents/Escape/world.py", line 127, in check_if_trade
self.trade(buyer=player, seller=self.trader)

File "/Users/Cpt_Chirp/Documents/Escape/world.py", line 96, in trade
print("{}. {} - {} Gold".format(i, item.name, item.value))
AttributeError: type object 'LargeManaPotion' has no attribute 'name'

Process finished with exit code 1

Tags: nameinpyselfvalueusersdocumentsfile
2条回答

在您用注释指示错误的行上,请改为调用

print(item)

我想你会看到一些令人惊讶的结果。在一个提示下,我做了以下事情:

^{pr2}$

您似乎没有为您的程序提供完整的源代码(因此我无法验证),但我怀疑您看到的是“<;class…”行。既然我不得不猜测,我会说构建卖家库存的地方是指类本身

LargeManaPotion

而不是实例化(调用)它

LargeManaPotion()

我不相信您提供了正确的代码,但是您已经提供了足够的代码来确定这里发生了什么

a = list()
b = list
a.append(1)
b.append(1)

哪一个会引起错误?显然,附加到b。“list”类型的对象有一个“append”方法,而基类“type list”没有。在

在某个地方,您将类型LargeManaPotion分配给一个变量,并试图从中访问字段{}。但是类型本身没有这些字段。之所以可以这样做,是因为在python中,类是第一类对象,可以像其他对象一样传递


让我们看看更接近您的实时代码

^{pr2}$

现在问题在哪里?它们都是Pot的实例,不是吗?为什么只有最后一个会引发AttributeError?在

当然,因为它们不尽相同。前4项是类Pot的实例。从方法__new__返回,该方法是在类type Pot中定义的,当我在变量名后面使用“括号表示法”时,该方法将被调用。在运行时,python不知道变量“Pot”是什么。它恰好是一个类型变量,它的调用生成一个实例对象。在

最后一项是类“type Pot”的实例。它不是一个锅。这是一种类型。它的__class__属性不是Pot。它的__class__属性是type类型用于生成实例。在类型中“添加”是没有意义的。在


假设你在现实生活中有药剂。你可以用药水做事。你可以喝。你可以检查他们的沸点(如果他们有标签,或者通过科学)。在

相反,假设你有一种药剂的配方。你说:“喝这个食谱”配方的沸点是多少。宇宙正在回应:“那是未定义的”。你是想看看药剂。相反,你看了它的配方。像所有的OO隐喻一样,这一个是不完整的。附加阅读:

相关问题 更多 >

    热门问题