iterable and iterator in Python

Summary

Everything you can loop over is an iterable. Looping over iterables works via getting an iterator from an iterable and then repeatedly asking the iterator for the next item.

The way iterators and iterables work is called the iterator protocol. List comprehensions, tuple unpacking, for loops, and all other forms of iteration rely on the iterator protocol.


Iterable

  1. In the Python world, an iterable is any object that you can loop over with a for loop
  2. Iterables are not always indexable, they don’t always have lengths, and they’re not always finite.;

Iterable & Iterator

  1. All iterables can be passed to the built-in iter function to get an iterator from them;
  2. Iterators have exactly one job: return the “next” item in our iterable;
  3. So iterators can be passed to the built-in next function to get the next item from them and if there is no next item (because we reached the end), a StopIteration exception will be raised;

The Iterator Protocol

The iterator protocol is a fancy term meaning “how iterables actually work in Python”.

Let’s redefine iterables from Python’s perspective.

Iterable:

1.Can be passed to the iter function to get an iterator for them
2.There is no 2. That’s really all that’s needed to be an iterable

Iterator:

1.Can be passed to the next function which gives their next item or raises StopIteration
2.Return themselves when passed to the iter function

The inverse of these statements should also hold true. Which means:

1.Anything that can be passed to iter without an error is an iterable.
2.Anything that can be passed to next without an error (except for StopIteration) is an iterator
3.Anything that returns itself when passed to iter is an iterator


Looping with iterators

This while loop manually loops over some iterable, printing out each item as it goes:

def print_each(iterable):
    iterator = iter(iterable)
    while True:
        try:
            item = next(iterator)
        except StopIteration:
            break
        else:
            print(item)

We can call this function with any iterable and it will loop over it:

>>> print_each({1, 2, 3})
1
2
3

The above function is essentially the same as this one which uses a for loop:

def print_each(iterable):
    for item in iterable:
        print(item)

This for loop is automatically doing what we were doing manually: calling iter to get an iterator and then calling next over and over until a StopIteration exception is raised.

Using the iterator protocol (either manually or automatically) is the only universal way to loop over any iterable in Python.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值