2017-09-27 15 views
-1

我知道已經有很多人問這個問題看起來像這樣,但我真的找不到一個適合我的解決方案。'模塊'的對象沒有屬性'scan'.narn python困難的方式ex48

在ex48。我寫的 「lexicon.py」 和nosetests它。但是它報告錯誤:

Traceback (most recent call last): 
    File "c:\python27\lib\site-packages\nose\case.py", line 197, in runTest 
    self.test(*self.arg) 
    File "D:\pyhomework\ex48\skeleton\tests\lexicon_tests.py", line 31, in test_errors 
    assert_equal(Lexicon.scan("ASDFASDF"),[('errors','ASDFASDF')]) 

AttributeError的: '模塊' 對象有沒有屬性 '掃描'

這是lexicon.py:

class Lexicon(object): 

    def __init__(self): 
     self.dic = {'direction':('north','south','east'), 
      'verb':('go','kill','eat'), 
      'stop':('the','in','of'), 
      'noun':('bear','princess'), 
      'number':('1234','3','91234'), 
      'error':('ASDFASDF','IAS')} 

    def scan(self,words): 
     self.word = words.split() 
     self.result = [] 
     for item in self.word: 
      for key,value in self.dic.items(): 
       if item in value: 
        self.result.append((key,item)) 
     return self.result 

這是lexicon_tests.py

from nose.tools import * 
from ex48 import Lexicon 


def test_direction(): 
    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("ASDFASDF"),[('errors','ASDFASDF')]) 
    result = Lexicon.scan("bear IAS princess") 
    assert_equal(result,[('noun','bear')('error','IAS')('noun','princess')]) 

這是骨架:

-bin 
-docs 
-ex48 (-__init__.py -Lexicon.py)    
-tests (-__init__.py -lexicon_tests.py) 
-setup 

我已經嘗試了幾種方法,但仍然得到相同的錯誤。 感謝您的任何建議。

回答

0

當您編寫from ex48 import Lexicon導入的是Lexicon.py文件作爲python模塊。但是,該模塊包含一個Lexicon類,其中包含您要使用的方法。

要擺脫您的錯誤,請嘗試以下導入語句:from ex48.lexicon import Lexicon這樣您將使用在lexicon.py文件中定義的Lexicon對象。

0
from ex48 import Lexicon 

所以你導入的模塊lexicon.py

在你模塊lexicon.py你有Lexicon

所以調用方法的類的Lexicon你必須使用符號

Lexicon.Lexicon.method() 
相關問題