什麼是可迭代的

Iterable 可以是逐個接收物品的任何物品。內建的 Python 集合是可迭代的:

[1, 2, 3]     # list, iterate over items
(1, 2, 3)     # tuple
{1, 2, 3}     # set
{1: 2, 3: 4}  # dict, iterate over keys

生成器返回迭代:

def foo():  # foo isn't iterable yet...
    yield 1

res = foo()  # ...but res already is