defining the behavior of zip(it, it) (WAS: Converting a flat list...)
bonono at gmail.com
bonono at gmail.com
Wed Nov 23 03:06:24 CET 2005
rhettinger at gmail.com wrote:
> IIRC, this was discussednd rejected in an SF bug report. It should not
> be a defined behavior for severals reasons:
> * It is not communicative to anyone reading the code that zip(it, it)
> is creating a sequence of the form (it0, it1), (it2, it3), . . . IOW,
> it conflicts with Python's style of plain-speaking.
> * It is too clever by far -- much more of a trick than a technique.
> * It is bug-prone -- zip(x,x) behaves differently when x is a sequence
> and when x is an iterator (because of restartability). Don't leave
> landmines for your code maintainers.
> * The design spirit of zip() and related functions is from the world of
> functional programming where a key virtue is avoidance of side-effects.
> Writing zip(it, it) is exploiting a side-effect of the implementation
> and its interaction with iterator inputs. The real spirit of zip() is
> having multiple sequences translated to grouped sequences out -- the
> order of application (and order of retrieving inputs) should be
> * Currently, a full understanding of zip() can be had by remembering
> that it maps zip(a, b) to (a0, b0), (a1, b1), . . . . That is simple
> to learn and easily remembered. In contrast, it introduces unnecessary
> complexity to tighten the definition to also include the order of
> application to cover the special case of zip being used for windowing.
> IOW, making this a defined behavior results in making the language
> harder to learn and remember.
> Overall, I think anyone using zip(it,it) is living in a state of sin,
> drawn to the tempations of one-liners and premature optimization. They
> are forsaking obvious code in favor of screwy special cases. The
> behavior has been left undefined for a reason.
While I agree that zip() should be left as it is(the definition as it
is only supposed to do a lock step of N iterables), I don't think
zip(it,it) is purely for the sake of one liner. It does convey the
intend clearer to me than for loop(well that IMO is the reasons of
many one liner). It tells me exactly what I want:
make a "window/mask" of N slots and put it on top of a list and fold.
The result are strips of the list each with element N.
zip(it,it,it,it) is a mask of 4 slot
zip(it,it,it,it,it) is a mask of 5 slot
Though in this case it is still not 100% correct as if my list has odd
number of elements, zip(it,it) would drop the last one, but that was
not defined in the original question.
More information about the Python-list