Special Method Protocols

__iter__() — containers protocol

Return an iterator for iter(obj). Containers normally return a fresh iterator; iterators return themselves. The return must implement the iterator protocol, not merely be an iterable.

Formal Syntax & Structure

python syntaxpython
def __iter__(self):

Return Value

iterator

Iterator over elements.

Working Example

Example Snippetpython
class Bag:    def __iter__(self): return iter((4,7))print(list(Bag()))
Python Reference