defining the behavior of zip(it, it) (WAS: Converting a flatlist...)

Fredrik Lundh fredrik at pythonware.com
Wed Nov 23 17:55:35 CET 2005


Steven Bethard wrote:

> Then why document itertools.izip() as it is?  The documentation there is
> explicit enough to know that izip(it, it) will work as intended.  Should
> we make the documentation there less explicit to discourage people from
> using the izip(it, it) idiom?

depends on whether you interpret "equivalent" as "having similar effects"
or "corresponding or virtually identical especially in effect or function" or
if you prefer some other dictionary definition...

because there are of course plenty of subtle differences between a Python
generator C type implementation.  let's see...

>>> from itertools import izip as izip1
>>> from library documentation import izip as izip2

>>> izip1
<type 'itertools.izip'>
>>> izip2
<function izip2 at 0x00A26670>

alright, close enough.

>>> izip1.func_name
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: type object 'itertools.izip' has no attribute 'func_name'
>>> izip2.func_name
'izip'

hmm.

>>> class myiter:
...     def next(self):
...             raise ValueError("oops!")
...
>>> izip2(myiter())
<generator object at 0x00A2AB48>
>>> izip1(myiter())
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: izip argument #1 must support iteration

oops.

>>> class myiter:
...     def __iter__(self):
...             return self
...     def next(self):
...             raise ValueError("oops!")
...
>>> izip1(myiter())
<itertools.izip object at 0x00A2AC88>
>>> izip2(myiter())
<generator object at 0x00A2AB48>

that's better.  now let's run it:

>>> list(izip1(myiter()))
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 5, in next
ValueError: oops!
>>> list(izip2(myiter()))
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "i.py", line 6, in izip2
    result = [i.next() for i in iterables]
  File "<stdin>", line 5, in next
ValueError: oops!

different stack depths.  hmm.

so how equivalent must something be to be equivalent?

</F> 






More information about the Python-list mailing list