Python:简单类型错误

标签 python typeerror

这是一个包含两个类的模块:

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

错误告诉我 Note.match 中的 filter 必须是 string,但是当我执行 n.search( "hello")"hello" 已经是一个字符串。

有人能告诉我里面到底出了什么问题吗? 谢谢!!!

最佳答案

您正在使用过滤器,但该变量在函数签名中声明为filer:

def search(self, filer):

因此,它可能需要来自外部作用域的不同filter(感谢@grc - filter 是一个内置函数),该过滤器未定义为字符串。

关于Python:简单类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25541890/

相关文章:

javascript - 如何正确使用appendChild

python - Numba jit : "Typing error" and "All templates rejected with/without literals" 问题

python - 使用 get_text 时“NoneType”对象不可调用 beautifulsoup 错误

python - ZMQ 套接字类型错误 : unicode strings only Error: is there a fix?

python - AWS lambda 通过 SES 发送带附件的电子邮件 > ParamValidationError : Parameter validation failed

python - 如何使用flask_restplus为swagger设置基本url?

Python 线程和 SQL

javascript - Moment Timezone 在加载时返回 Uncaught TypeError

python - 如何安装包含 sqlite3 数据库连接的 python 包?

python - 如何在不更改格式的情况下从txt文件中删除标点符号