2014-08-28 84 views
1

这是含有两个类的模块:的Python:简单类型错误

import datetime 

# Store the next available id for all new notes 

last_id = 0 

class Note: 
    '''Represent a note in the notebook. Match against a string 
    in searches and store tags for each note.''' 

    def __init__(self, memo, tags=''): 
     '''initialize a note with memo and optional 
     space-sparated tags. Automatically set the note's 
     creation date and a unique id.''' 
     self.memo = memo 
     self.tags = tags 
     self.creation_date = datetime.date.today() 
     global last_id 
     last_id += 1 
     self.id = last_id 

    def match(self, filter): 
     '''Determine if this note matches the filter 
     text. Return True if it matches, False otherwise. 

     Search is case sensitive and matches both text and tags.''' 
     return filter in self.memo or filter in self.tags 

class Notebook: 
    '''Represent a collection of notes that can be tagged, 
    modified, and searched.''' 

    def __init__(self): 
     '''Initialize a notebook with an empty list.''' 
     self.notes = [] 

    def new_note(self, memo, tags = ''): 
     '''Create a new note and add it to the list.''' 
     self.notes.append(Note(memo, tags)) 

    def modify_memo(self, note_id, memo): 
     '''Find the note with the given id and change its 
     memo to the given value.''' 
     for note in self.notes: 
      if note.id == note_id: 
       note.memo = memo 
       break 

    def modify_tags(self, note_id, tags): 
     '''Find the note with the given id and change its 
     tags to the given value.''' 
     for note in self.notes: 
      if note.id == note_id: 
       note.tags = tags 
       break 

    def search(self, filer): 
     '''Find all notes that match the given filter 
     string.''' 
     return [note for note in self.notes if note.match(filter)] 

当我创建的Notebook()一个实例,并执行search("hello"),一个TypeError发生了。

>>> from notebook import Note, Notebook 
>>> n = Notebook() 
>>> n.new_note("hello world") 
>>> n.new_note("hello again") 
>>> n.notes 
[<notebook.Note object at 0x02A2F5F0>, <notebook.Note object at 0x02A51D90>] 
>>> n.search("hello") 
Traceback (most recent call last): 
    File "<pyshell#30>", line 1, in <module> 
    n.search("hello") 
    File "C:/Users/James/Desktop/Python3OOP/Cahpter2Notebook\notebook.py", line 60, in search 
    return [note for note in self.notes if note.match(filter)] 
    File "C:/Users/James/Desktop/Python3OOP/Cahpter2Notebook\notebook.py", line 60, in <listcomp> 
    return [note for note in self.notes if note.match(filter)] 
    File "C:/Users/James/Desktop/Python3OOP/Cahpter2Notebook\notebook.py", line 27, in match 
    return filter in self.memo or filter in self.tags 
TypeError: 'in <string>' requires string as left operand, not type 

错误tolds我Note.matchfilter必须是string,但是当我执行n.search("hello")"hello"已经是一个字符串。

有人能告诉我什么是真正的错误? Thx !!!

+1

避免使用'filter'作为变量名称。这是一个内置的python名称 – shaktimaan 2014-08-28 06:18:32

回答

2

您使用的是filter但变量是在函数的签名声明为filer

def search(self, filer): 

所以它可能需要外部范围不同filter(感谢@grc - 过滤器是内置函数),它没有被定义为一个字符串。

+1

['filter'](https://docs.python.org/3/library/functions.html#filter)是一个内置函数。 – grc 2014-08-28 06:16:48

+0

@grc使这种犯罪更加糟糕 - 大多数IDE将突出保留字,事实上,您可以(重写保留的关键字)并不意味着您应该... – alfasin 2014-08-28 06:19:46

+0

我使用Python的内置IDE进行此练习。你能告诉我你们怎么能这么快找到这种打字错误? – user2988464 2014-08-28 06:21:10