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

rurpy at yahoo.com rurpy at yahoo.com
Tue Nov 22 21:24:04 EST 2005


rhettinger at gmail.com wrote:
> >  > ii. The other problem is easier to explain by example.
> >  > Let it=iter([1,2,3,4]).
> >  > What is the result of zip(*[it]*2)?
> >  > The current answer is: [(1,2),(3,4)],
> >  > but it is impossible to determine this from the docs,
> >  > which would allow [(1,3),(2,4)] instead (or indeed
> >  > other possibilities).
> >  > """
> >  > IMO left->right is useful enough to warrant making it defined
> >  > behaviour
> >
> > And in fact, it is defined behavior for itertools.izip() [1].
> >
> > I don't see why it's such a big deal to make it defined behavior for
> > zip() too.
>
> 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.

Seems pretty plain to me, and I am far from a Python expert.

> * It is too clever by far -- much more of a trick than a technique.

I see tons of tricks posted every day.  There is a time and place
for them.

> * 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.

Err thanks for the advice, but they are *my* code maintainers and
I am the best judge of what constitutes a landmine.

> * 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
> irrelevant.

I'm not sure what to say when people start talking about "spirit"
when making technical decisions.

> * 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.

I don't see how defining zip()'s behavior more precisely, interfers
at all with this understanding.

> 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.

I really don't understand this point of view.  It is exactly what
I was just complaining about in a different thread.  Every one
your reasons (except the last, which is very weak anyway) is
based on how you think someone may use zip if you define
fully what it does.

Why do you feel compelled to tell me how I should or
shouldn't write my code?!

It is this attitude of "we know what is best for you, child"
that really pisses me off about Python sometimes.  Please
make language decisions based on technical considerations
and not how you want me to use the language.  Believe it
or not, I can make those decisions on my own.




More information about the Python-list mailing list