我正在尝试测试以下简单对象:
class WebCorpus(object):
def __init__(self):
_index = {}
_graph = {}
_ranks = {}
_corpusChanged = False
def lookup(self, keyword):
if keyword in _index:
return _index[keyword]
return None
# (some irrelevant code)
和:
from WebCorpus import WebCorpus
def test_engine():
print "Testing..."
content = """This is a sample <a href="http://www.example.com">webpage</a> with
<a href="http://www.go.to">two links</a> that lead nowhere special."""
outlinks = ["http://www.example.com", "http://www.go.to"]
corpus = WebCorpus()
assert corpus.lookup("anything") == None
#(some more code)
test_engine()
但它给了我一个错误: NameError: global name '_index' is not defined。我不明白这一点,_index 在__init__
!?我的错误是什么?帮助表示赞赏。