0
class Inventory(object):
    def __init__(self):
        self.inventory = {
            'cash': 500,
            'paycheck': 1,
            'savings': 1000,
            'current_car': 0,
            'possible_cars': ['Chevy', 'Honda', 'BMW'],
            'car_cost': [0, 100, 200],
            'current_house': 0,
            'possible_houses': ['apartment','townhouse','suite'],
            'house_cost': [0, 150, 300],
            'status': self.status()
                  }

    def status(self):
        while self.inventory['cash'] + self.inventory['savings'] > 0:
            return True

我目前正在完成练习 45 中的“Learn Python the Hard Way”。我创建了一个类来列出与我的游戏相关的项目,我已经将这些键和值存储在我的init方法下的字典中。我遇到问题的地方是我的最后一个键和它的值——“状态”键。

我想要这个值做的是引用我的状态方法,只要我的玩家有一个正数的钱,我就将它设置为返回真(我的游戏的其他部分将引用 .inventory['status'] 来检查在它们执行之前它的真实性现在我已经快速完成了两行概念验证代码来验证是否可以将函数用作值 - 我要挂断的是如何在类中实现它,特别是当我的字典在init中时。

我的错误:

Traceback (most recent call last):
  File "ex45file1.py", line 151, in <module>
    my_inv  = Inventory() #TEST
  File "ex45file1.py", line 80, in __init__
    'status': status()
NameError: global name 'status' is not defined

我在哪里错了?

4

3 回答 3

4

首先,这不是您的代码产生的错误。在您的版本中,您拥有'status': status()但在 SO 上您写了'status': self.status(). 无论如何,如果你解决了你仍然有问题,

AttributeError: 'Inventory' object has no attribute 'inventory'

您收到该错误的原因是因为 Python 正在定义您的inventory属性,但您被调用必须引用它status以提供返回值。inventory

您甚至不想调用该函数并将返回值保存在字典中,因为这不允许您动态使用它。你应该改变它,这样你就不会调用,而只是保存引用。

class Inventory(object):
    def __init__(self):
        self.inventory = {
            'cash': 500,
            'paycheck': 1,
            'savings': 1000,
            'current_car': 0,
            'possible_cars': ['Chevy', 'Honda', 'BMW'],
            'car_cost': [0, 100, 200],
            'current_house': 0,
            'possible_houses': ['apartment','townhouse','suite'],
            'house_cost': [0, 150, 300],
            'status': self.status  # <--- don't use parens ()
        }

只需调用该方法,

>>> my_inventory = Inventory()
>>> my_inventory.inventory['status']()
True
于 2013-07-11T02:22:50.577 回答
1

我得到了一个不同的错误,但我相信解决方案是一样的:

class Inventory(object):
    def __init__(self):
        self.inventory = {
            'cash': 500,
            'paycheck': 1,
            'savings': 1000,
            'current_car': 0,
            'possible_cars': ['Chevy', 'Honda', 'BMW'],
            'car_cost': [0, 100, 200],
            'current_house': 0,
            'possible_houses': ['apartment','townhouse','suite'],
            'house_cost': [0, 150, 300],
                  }
        self.inventory['status'] = self.status()

    def status(self):
        while self.inventory['cash'] + self.inventory['savings'] > 0:
            return True

我的错误是抱怨未在 status() 中定义库存。

于 2013-07-11T02:26:14.530 回答
0

我尝试将您的代码复制并粘贴到我的 Python 解释器中,但我得到了一个不同的错误:

>>> inv = new Inventory()
  File "<stdin>", line 1
    inv = new Inventory()
                      ^
SyntaxError: invalid syntax
>>> inv = Inventory()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 13, in __init__
  File "<stdin>", line 16, in status
AttributeError: 'Inventory' object has no attribute 'inventory'

问题是您在inventory和之间存在循环依赖关系status。您使用statusto define inventory,但status需要先阅读inventory才能返回...看到问题了吗?

于 2013-07-11T02:23:27.933 回答