Python变量没有用所需的对象初始化,为什么?

2024-04-29 13:30:26 发布

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

所以我正在设计一个战舰后端系统,将运行在谷歌应用引擎。。。我昨天刚开始设计了一个游戏框架。在

不幸的是,我没有用python编写太多代码,因此对该语言的细节不太熟悉。当我试图运行程序时,我总是收到以下错误:

 File "C:\Users\Shlomo\Desktop\eclipse\plugins\org.python.pydev_2.7.5.2013052819\pysrc\pydev_runfiles.py", line 432, in __get_module_from_str
mod = __import__(modname)
File "C:\Users\Shlomo\workspace\battleship\Battleship.py", line 222, in <module>
battleship = BattleshipGame("Shlomo",1,1,4,1,1,2,5,2,1,3,3,3,1,4,2,4,1,5,5,5,"John",1,1,4,1,1,2,5,2,1,3,3,3,1,4,2,4,1,5,5,5)
File "C:\Users\Shlomo\workspace\battleship\Battleship.py", line 210, in __init__
field = self.player1_field.getField()
AttributeError: 'NoneType' object has no attribute 'getField'
ERROR: Module: Battleship could not be imported (file: C:\Users\Shlomo\workspace\battleship\Battleship.py).

所以我把这个错误翻译成变量字段没有被PlayerField对象初始化。。。在

这是我的代码:

^{pr2}$

抱歉,关于sytax,我无法粘贴到代码格式。这个代码可能有一些问题,但我还没能通过这个问题。。。在

我做错什么了?在


Tags: 代码inpyfield错误lineusersworkspace
2条回答

Python不是Java,也不是您要编写的任何语言。这些可变的类属性不太可能像您认为的那样。在

 class PlayerField:
    player_field = [[0 for i in xrange(10)] for i in xrange(10)]
    shipsOnField = []
    goodToGo = False

__init__中,将赋值给局部变量player1_字段,而不是实例数据。而不是:

player1_field = PlayerField(...

你想要:

^{pr2}$

如果不这样做,您将尝试尊重类对于player1_字段的值,即None。在

相关问题 更多 >