Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将方法用作字典值_Python_Python 2.7 - Fatal编程技术网

Python 将方法用作字典值

Python 将方法用作字典值,python,python-2.7,Python,Python 2.7,我目前正在练习45的“艰苦学习Python”。我创建了一个类来列出与我的游戏相关的项目,我将这些键和值存储在我的init方法下的字典中。我遇到问题的地方是我的最后一个键和它的值——“状态”键 我想让这个值做的是引用我的status方法,只要我的玩家有一笔正的钱,我就将其设置为返回true(游戏的其他部分将引用。inventory['status']在它们执行之前检查它的真实性。现在我已经完成了快速的两行概念验证代码,以验证是否可以使用函数作为值-我要讨论的是如何在类中实现这一点,特别是当我的字典

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

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

我的错误:

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
回溯(最近一次呼叫最后一次):
文件“ex45file1.py”,第151行,在
我的库存=库存()#测试
文件“ex45file1.py”,第80行,在_init中__
“状态”:状态()
NameError:未定义全局名称“状态”

我哪里出错了?

首先,这不是您的代码产生的错误。在您的版本中,您有
'status':status()
,但是您编写了
'status':self.status()
。在任何情况下,如果您修复了仍然存在的问题

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
出现该错误的原因是,Python正在定义
库存
属性,但您需要调用
状态
,它必须引用
库存
以给出返回值

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

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

首先,这不是代码产生的错误。在您的版本中,您有
'status':status()
,但是您写了
'status':self.status()
。在任何情况下,如果您解决了这个问题

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
出现该错误的原因是,Python正在定义
库存
属性,但您需要调用
状态
,它必须引用
库存
以给出返回值

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

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

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

>>> my_inventory = Inventory()
>>> my_inventory.inventory['status']()
True
>库存=新库存()
文件“”,第1行
存货=新存货()
^
SyntaxError:无效语法
>>>存货=存货()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“”,第13行,在_init中__
文件“”,第16行,处于状态
AttributeError:“库存”对象没有属性“库存”

问题是
inventory
status
之间存在循环依赖关系。您使用
status
定义
inventory
,但是
status
需要读取
inventory
才能返回…看到问题了吗?

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

>>> my_inventory = Inventory()
>>> my_inventory.inventory['status']()
True
>库存=新库存()
文件“”,第1行
存货=新存货()
^
SyntaxError:无效语法
>>>存货=存货()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“”,第13行,在_init中__
文件“”,第16行,处于状态
AttributeError:“库存”对象没有属性“库存”

问题是在
库存
状态
之间存在循环依赖关系。您使用
状态
定义
库存
,但是
状态
需要读取
库存
才能返回…看到问题了吗?

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

>>> 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'

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

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

>>> 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'

我的错误是抱怨未在状态()中定义库存.

您确定Python使用的是您模块的最新版本吗?请尝试重新保存并重新运行。只需确保它已保存并运行了两次。相同的结果检查Python使用的文件是否与您发布的代码相同。特别是,确保它确实显示了
self.status()
而不是
status()
。抱歉,看起来我确实切换了代码/错误。这与错误无关,但您的
状态
方法中的
while
循环似乎正在执行
if
语句的工作。它最多只运行一次套件,因为套件中唯一的语句是
return
。(这可能是一件好事,因为如果不返回,循环将永远重复,因为您也不会修改它在其条件中检查的变量)。如果条件失败,您可能还希望返回默认值以外的值
None
。您可以使用
return self.invetory['cash']+self.inventory['savings']>0
,它将始终返回
True
False
您确定Python正在使用模块的最新版本吗?请尝试重新保存并重新运行。只需确保它已保存并运行了两次。相同的结果检查Python正在使用的文件是否与您发布的代码相同。特别是,确保它确实显示
 self.status()
而不仅仅是
status()
。抱歉,看起来我确实切换了代码/错误。这与错误无关,但您的
状态
方法中的
while
循环似乎正在执行
if
语句的工作。它最多只运行一次套件,因为套件中唯一的语句是
return
。(这也许是件好事,si