1

This is a module containing two class:

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)]

When I create an instance of Notebook(), and perform a search("hello"), a TypeError happened.

>>> 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

The error tolds me the filter in Note.match must be a string, but when I perform n.search("hello"), "hello" is already a string.

Can someone tell me what's really wrong inside? Thx!!!

1
  • 1
    Avoid using filter as a variable name. It is the name of a python built-in Commented Aug 28, 2014 at 6:18

1 Answer 1

2

You're using a filter but the variable is declared in the function's signature as filer:

def search(self, filer):

so it probably takes a different filter from an outer scope (thanks to @grc - filter is a builtin function) that is not defined as a string.

Sign up to request clarification or add additional context in comments.

3 Comments

filter is a built-in function.
@grc which makes this crime even worse - most IDEs will highlight reserved words and the fact that you could (override a reserved keyword) doesn't mean you should...
i'm using Python's built-in IDE for this practise. Can you tell me how come you guys can find this typing error so fast?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.