Skip to main content
 首页 » 编程设计

python之无法导入 Python 类

2025年05月04日12lautakyan007

我正在使用 Python 2.7,并尝试浏览学习指南艰难地学习 Python。我在尝试练习 49 时遇到问题。

Python 2.7.10 (default, Jul 14 2015, 19:46:27)  
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin 
Type "help", "copyright", "credits" or "license" for more information. 
 
>>> from ex48.parser import * 
>>> x = parse_sentence([('verb', 'run'), ('direction', 'north')]) 
>>> x.subject 
'player' 
>>> x.verb 
'run' 
>>> x.object 
'north' 
>>> x = parse_sentence([('noun', 'bear'), ('verb', 'eat'), ('stop', 'the'), ('noun', 'honey')]) 
>>> x.subject 
'bear' 
>>> x.verb 
'eat' 
>>> x.object 
'honey' 

当我这样做时:

from ex48.parser import * 
x = parse_sentence([('verb', 'run'), ('direction', 'north')]) 

Windows PowerShell 给我:

Traceback (most recent call last): 
File "<stdin>", line 1, in <module> 
NameError: name 'parse_sentence' is not defined 

我的文件结构是:

../lexicon 
     bin/ 
     docs/ 
     ex48/ 
         __init__.py 
         lexicon.py 
         parser.py 
     setup.py 
     tests/ 
         lexicon_tests.py 
         __init__.py 

这是parser.py中的代码

class ParserError(Exception): 
    pass 
 
class Sentence(object): 
    def __init__(self, subject, verb, obj): 
        #remember we take ('noun', 'princess') tuples and convert them 
        self.subject = subject[1] 
        self.verb = verb[1] 
        self.object = obj[1] 
 
    def peek(word_list): 
        if word_list: 
            word = word_list[0] 
            return word[0] 
        else: 
            return None 
 
    def match(word_list, expecting): 
        if word_list: 
            word = word_list.pop(0) 
 
            if word[0] == expecting: 
                return word 
            else: 
                return None 
        else: 
            return None 
 
    def skip(word_list, word_type): 
        while peek(word_list) == word_type: 
            match(word_list, word_type) 
 
    def parse_verb(word_list): 
        skip(word_list, 'stop') 
 
        if peek(word_list) == 'verb': 
            return match(word_list, 'verb') 
        else: 
            raise ParserError("Expected a verb next") 
 
    def parse_object(word_list): 
        skip(word_list, 'stop') 
        next_word = peek(word_list) 
 
        if next_word == 'noun': 
            return match(word_list, 'noun') 
        elif next_word == 'direction': 
            return match(word_list, 'direction') 
        else: 
            raise ParserError("Expected a noun or direction next.") 
 
    def parse_subject(word_list): 
        skip(word_list, 'stop') 
        next_word = peek(word_list) 
 
        if next_word == 'noun': 
            return match(word_list, 'noun') 
        elif next_word == 'verb': 
            return ('noun', 'player') 
        else: 
            raise ParserError("Expected a verb next.") 
 
    def parse_sentence(word_list): 
        subj = parse_subject(word_list) 
        verb = parse_verb(word_list) 
        obj = parse_object(word_list) 
 
        return Sentence(subj, verb, obj) 

请您参考如下方法:

错误的身份。 只有部分类的第一个函数。

class ParserError(Exception): 
    pass 
 
class Sentence(object): 
    def __init__(self, subject, verb, obj): 
        #remember we take ('noun', 'princess') tuples and convert them 
        self.subject = subject[1] 
        self.verb = verb[1] 
        self.object = obj[1] 
 
def peek(word_list): 
    if word_list: 
        word = word_list[0] 
        return word[0] 
    else: 
        return None 
 
def match(word_list, expecting): 
    if word_list: 
        word = word_list.pop(0) 
 
        if word[0] == expecting: 
            return word 
        else: 
            return None 
    else: 
        return None 
 
def skip(word_list, word_type): 
    while peek(word_list) == word_type: 
        match(word_list, word_type) 
 
def parse_verb(word_list): 
    skip(word_list, 'stop') 
 
    if peek(word_list) == 'verb': 
        return match(word_list, 'verb') 
    else: 
        raise ParserError("Expected a verb next") 
 
def parse_object(word_list): 
    skip(word_list, 'stop') 
    next_word = peek(word_list) 
 
    if next_word == 'noun': 
        return match(word_list, 'noun') 
    elif next_word == 'direction': 
        return match(word_list, 'direction') 
    else: 
        raise ParserError("Expected a noun or direction next.") 
 
def parse_subject(word_list): 
    skip(word_list, 'stop') 
    next_word = peek(word_list) 
 
    if next_word == 'noun': 
        return match(word_list, 'noun') 
    elif next_word == 'verb': 
        return ('noun', 'player') 
    else: 
        raise ParserError("Expected a verb next.") 
 
def parse_sentence(word_list): 
    subj = parse_subject(word_list) 
    verb = parse_verb(word_list) 
    obj = parse_object(word_list) 
 
    return Sentence(subj, verb, obj)