Python iteration over non-sequence

后端 未结 4 1168
温柔的废话
温柔的废话 2021-01-04 01:16

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 Not         


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-04 01:59

    Notes is an instance of NoteBook. To iterate over such an object, it needs an __iter__ method:

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

    PS. It is a PEP8 recommendation/convention in Python to use lowercase variable names for instances of classes, and CamelCase for class names. 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.

提交回复
热议问题