简体   繁体   English

Python迭代非序列

[英]Python iteration over non-sequence

I have this piece of code which creates a note and adds to the notebook. 我有这段代码创建一个笔记并添加到笔记本中。 When I run this I get a Iteration over non-sequence error. 当我运行这个时,我得到一个非序列错误的迭代。

import datetime
class Note:
    def __init__(self, memo, tags):
        self.memo = memo
        self.tags = tags
        self.creation_date = datetime.date.today()

def __str__(self):
    return 'Memo={0}, Tag={1}'.format(self.memo, self.tags)


class NoteBook:
     def __init__(self):
        self.notes = []

     def add_note(self,memo,tags):
        self.notes.append(Note(memo,tags))

if __name__ == "__main__":
    firstnote = Note('This is my first memo','example')
    print(firstnote)
    Notes = NoteBook()
    Notes.add_note('Added thru notes','example-1')
    Notes.add_note('Added thru notes','example-2')
    for note in Notes:
        print(note.memo)

Error: 错误:

C:\Python27\Basics\OOP\formytesting>python notebook.py  
Memo=This is my first memo, Tag=example  
Traceback (most recent call last):  
  File "notebook.py", line 27, in   
    for note in Notes:  
TypeError: iteration over non-sequence

You are trying to iterate over the object itself, which is returning the error. 您正在尝试迭代对象本身,这将返回错误。 You want to iterate over the list inside the object, in this case Notes.notes (which is somewhat confusing naming, you may want to distinguish the internal list by using another name for the instance of the notebook object). 您希望迭代对象内的列表,在本例中为Notes.notes (这有点令人困惑的命名,您可能希望通过使用笔记本对象实例的另一个名称来区分内部列表)。

for note in Notes.notes:
    print(note.memo)

Notes is an instance of NoteBook . Notes是NoteBook一个实例。 To iterate over such an object, it needs an __iter__ method : 要迭代这样的对象,它需要一个__iter__方法

class NoteBook:

    def __iter__(self):
        return iter(self.notes)

PS. PS。 It is a PEP8 recommendation/convention in Python to use lowercase variable names for instances of classes, and CamelCase for class names. Python中的PEP8建议/约定是对类的实例使用小写变量名,对类名使用CamelCase。 Following this convention will help you instantly recognize your class instances from your classes. 遵循此约定将帮助您立即从类中识别您的类实例。

If you wish to follow this convention (and endear yourself to others who like this convention), change Notes to notes . 如果您希望遵循此约定(并且喜欢其他喜欢此约定的人),请将Notes更改为notes

If you wanted to actually iterate Notes itself, you could also add a custom __iter__ method to it that returns the .notes property. 如果你想实际迭代Notes本身,你也可以__iter__ method to it that returns the .notes property.添加一个__iter__ method to it that returns the .notes property.的自定义__iter__ method to it that returns the .notes property.

class Notebook:

    def __iter__(self):
        return iter(self.notes)

    ...

The problem is in the line for note in Notes: since Notes is an object not a list. 问题出现for note in Notes:for note in Notes:因为Notes是一个对象而不是列表。 I believe you want for note in Notes.notes: 我相信你想for note in Notes.notes:

also as unutbu pointed out, you can overload the __iter__ operator which will allow your current loop to work. 同样unutbu指出,你可以重载__iter__运算符,这将允许你的当前循环工作。 It depends how you want this to outwardly appear. 这取决于你希望它如何向外出现。 Personally I would overload __iter__ 我个人会超载__iter__

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM