BrenBarn非常愉快地回答了#1,但我相信我已经解开了#2 的奥秘。也就是说,acallable-iterator
是返回以iter
与第二种形式一起使用的内容:
>>> help(iter)
iter(...)
iter(collection) -> iterator
iter(callable, sentinel) -> iterator
Get an iterator from an object. In the first form, the argument must
supply its own iterator, or be a sequence.
In the second form, the callable is called until it returns the sentinel.
以机智:
>>> def globals_are_bad_mmkay():
global foo
foo += 1
return foo
>>> foo = 0
>>> it = iter(globals_are_bad_mmkay, 10)
>>> it
<callable-iterator object at 0x021609B0>
>>> list(it)
[1, 2, 3, 4, 5, 6, 7, 8, 9]