module'对象不可下标"在这段代码中是什么意思?
运行 test.py 会得到
Traceback (most recent call last):
File "test.py", line 3, in <module>
Map = Parser.Map(os.path[0] + "\\start.wmap")
TypeError: 'module' object is not subscriptable
Parser.py
import configparser
def StringIsNumber(String):
try:
int(String)
except:
return False
return True
class Map:
Parser = configparser.RawConfigParser()
MapURL = ""
def __init__(self, Map):
self.Parser.read(Map)
self.MapURL = Map
def TileTypes(self):
#All numerical sections can be assumed to be tiles
return [n for n in self.Parser.sections() if StringIsNumber(n)]
test.py
import Parser
import os
Map = Parser.Map(os.path[0] + "\\start.wmap")
print(Map.TileTypes())
4 个回答
1
你试图用方括号去访问os.path,这其实是一个模块。用方括号访问某个对象叫做“下标”,比如字典(dict)这种对象是可以用方括号的,但模块就不行。
错误出现在 os.path[...]
这里。
2
你不能像这样通过键或索引来获取它的属性:something[property]
2
os.path
是一个模块,你现在把它当成了一个列表。我觉得你其实想要的是 sys.path
。