Skip to main content
 首页 » 编程设计

python之艰难地学习 Python : Exercise 48之AttributeError

2025年05月04日18Terrylee

我一直在尝试完成艰难地学习 Python,在练习 48 中,我在运行 nosetests 时继续遇到错误。我正在使用其他人已经在网站上验证过的代码工作,但无论我继续得到这个错误:

====================================================================== 
ERROR: tests.ex48_tests.test_directions 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/nose/case.py", line 197, in runTest 
        self.test(*self.arg) 
    File "/Users/AlexanderMariona/Documents/Home/Programming/Python/Projects/Exercise 48/tests/ex48_tests.py", line 6, in test_directions 
        assert_equal(lexicon.scan("north"), [('direction', 'north')]) 
AttributeError: 'module' object has no attribute 'scan' 

我遇到此错误 6 次,每个测试函数一次。

这是我在代码中使用的内容:

词典.py:

class Lexicon(object): 
 
    directions = ['north', 'south', 'east', 'west', 'down', 'up', 'down', 'right'] 
    verbs = ['go', 'stop', 'kill', 'eat'] 
    stops = ['the', 'in', 'at', 'of', 'from', 'at', 'it'] 
    nouns = ['door', 'bear', 'princess', 'cabinet'] 
 
    def scan(thewords): 
 
        thewords = thewords.split() 
        sentence = [] 
 
        for i in thewords: 
 
            if i in directions: 
                sentence.append(('direction', i)) 
 
            elif i in verbs: 
                sentence.append(('verb', i)) 
 
            elif i in stops: 
                sentence.append(('stop', i)) 
 
            elif i in nouns: 
                sentence.append(('noun', i)) 
 
            elif i.isdigit(): 
                sentence.append(('number', convert_number(i))) 
 
            else:             
                sentence.append(('error', i)) 
 
        return sentence 
 
    def convert_number(s): 
        try: 
            return int(s) 
 
        except ValueError: 
            return None 
 
lexicon = Lexicon() 

(这是 Dairylee 写的。)

ex48_tests.py:

from nose.tools import * 
from ex48 import lexicon 
 
 
def test_directions(): 
    assert_equal(lexicon.scan("north"), [('direction', 'north')]) 
    result = lexicon.scan("north south east") 
    assert_equal(result, [('direction', 'north'), 
                          ('direction', 'south'), 
                          ('direction', 'east')]) 
 
def test_verbs(): 
    assert_equal(lexicon.scan("go"), [('verb', 'go')]) 
    result = lexicon.scan("go kill eat") 
    assert_equal(result, [('verb', 'go'), 
                          ('verb', 'kill'), 
                          ('verb', 'eat')]) 
 
 
def test_stops(): 
    assert_equal(lexicon.scan("the"), [('stop', 'the')]) 
    result = lexicon.scan("the in of") 
    assert_equal(result, [('stop', 'the'), 
                          ('stop', 'in'), 
                          ('stop', 'of')]) 
 
 
def test_nouns(): 
    assert_equal(lexicon.scan("bear"), [('noun', 'bear')]) 
    result = lexicon.scan("bear princess") 
    assert_equal(result, [('noun', 'bear'), 
                          ('noun', 'princess')]) 
 
def test_numbers(): 
    assert_equal(lexicon.scan("1234"), [('number', 1234)]) 
    result = lexicon.scan("3 91234") 
    assert_equal(result, [('number', 3), 
                          ('number', 91234)]) 
 
 
def test_errors(): 
    assert_equal(lexicon.scan("ASDFADFASDF"), [('error', 'ASDFADFASDF')]) 
    result = lexicon.scan("bear IAS princess") 
    assert_equal(result, [('noun', 'bear'), 
                          ('error', 'IAS'), 
                          ('noun', 'princess')]) 

(这是从 LPTHW 逐字复制的。)

设置.py:

try: 
    from setuptools import setup 
except ImportError: 
    from distutils.core import setup 
 
config = { 
    'name': 'Excercise 48', 
    'description': 'LPTHW', 
    'version': '0.1', 
    'author': 'My Name', 
    'author_email': 'My E-Mail', 
    'url': 'None', 
    'download_url': 'None', 
    'packages': ['ex48'], 
    'scripts': [], 
    'install_requires': ['nose'] 
} 
 
setup(**config) 

这是包的目录:

Exercise 48/ 
    bin/ 
    docs/ 
    ex48/ 
        __init__.py 
        lexicon.py 
    setup.py 
    tests/ 
        __init__.py 
        ex48_tests.py 

究竟是什么导致了这个错误?

请您参考如下方法:

发生此错误是因为模块lexicon 中没有函数scan。 Lexicon 类中有一个方法,那么应该像方法一样调用它(注意缺少self 参数)。

另一方面,Lexicon 根本不必作为类存在,scan 可以是模块级函数。