这是我的代码中的错误部分:
class Room(object):
def __init__(self):
self.hotspots = []
self.image = []
self.item = []
def newhotspot(*args):
new = {'Name' : None,
'id' : None,
'rect' : None,
'Takeable' : None,
'Lookable' : None,
'Speakable' : None,
'Itemable' : None,
'Goable' : None}
for i in args:
new[i[0]] = i[1]
new['id'] = len(self.hotspots)
self.hotspots.append(new)
CityTrader = Room()
CityTrader.newhotspot(('Name', 'Trader'),
('Takeable', 'Yes'))
Run Code Online (Sandbox Code Playgroud)
目标是使用一个字典,将所有键设置为none,但指定的键.但是,当我运行它时,我得到:
[...]line 85 in <module>
('Takeable', 'Yes'))
[...]line 44, in newhotspot
new[i[0]] = i[1]
TypeError : 'Room' object does not support indexing
Run Code Online (Sandbox Code Playgroud)
谁知道为什么以及如何解决这个问题?它似乎在没有包装在一个类中时起作用.
你忘了self参数newhotspot():
def newhotspot(self, *args):
...
Run Code Online (Sandbox Code Playgroud)
因为self无论如何将隐式传递,它最终成为第一项args.
| 归档时间: |
|
| 查看次数: |
21874 次 |
| 最近记录: |