2013-12-23 3 views
1

저는 어려운 방법으로 파이썬 배우기 연습을하고 있습니다. 연습 문제 49 (http://learnpythonthehardway.org/book/ex49.html)와 두 개의 다른 객체를 비교해야합니다. 이것은 내가 시험에 필요한 코드 :코와 객체 비교

class ParserError(Exception): 
    pass 


class Sentence(object): 

def __init__(self, subject, verb, object): 
    # remember we take ('noun','princess') tuples and convert them 
    self.subject = subject[1] 
    self.verb = verb[1] 
    self.object = object[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) #Here it's returning 'noun' from the noun/princess tuple 

     if word[0] == expecting: #Here it's testing whether or not the new item in the  0 position ('noun' in this case) = 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 = peek(word_list) 

    if next == 'noun': 
     return match(word_list, 'noun') 
    if next == 'direction': 
     return match(word_list, 'direction') 
    else: 
     raise ParserError("Expected a noun or direction next.") 


def parse_subject(word_list, subj): 
    verb = parse_verb(word_list) 
    obj = parse_object(word_list) 

    return Sentence(subj, verb, obj) 


def parse_sentence(word_list): 
    skip(word_list, 'stop') 

    start = peek(word_list) 

    if start == 'noun': 
     subj = match(word_list, 'noun') 
     return parse_subject(word_list, subj) 
    elif start == 'verb': 
     # assume the subject is the player then 
     return parse_subject(word_list, ('noun', 'player')) 
    else: 
     raise ParserError("Must start with subject, object, or verb not: %s" % start) 

내 문제는 내가 문장 객체를 생성하는데있어 기능 parse_sentence 함께.

def test_parse_subject(): 
    word_list = [('verb', 'kill'), ('direction', 'north')] 

    subj = ('noun', 'princess') 
    verb = ('verb', 'kill') 
    obj = ('direction', 'north') 
    obj_sent = Sentence(subj, verb, obj) 

    assert_equal(parser.parse_subject(word_list, subj), obj_sent) 

하지만이 역 추적 오류가 계속 받기 : 그래서 내 테스트 코드에서 다른 문장 객체를 생성 할 필요가 있는지가 동일

Traceback (most recent call last): 
    File "G:\Python27\lib\site-packages\nose\case.py", line 197, in runTest 
    self.test(*self.arg) 
    File "G:\Users\Charles\dropbox\programming\parsing_test\skeleton\tests\parser_tests.py", line 45, in test_parse_subjec 
t 
    assert_equal(parser.parse_subject(word_list, subj), obj_sent) 
AssertionError: <ex49.parser.Sentence object at 0x02651C50> != <ex49.parser.Sentence object at 0x02651C30> 

그래서 동일하게 개체를 반환 아니에요을 , 그러나 나는 그들이 꽤다는 것을 확신한다. 나는 그들에게 올바른 주장을했다. 객체가 동일하면 어떻게 확인할 수 있습니까? 미리 감사드립니다.

답변