PEP 276 -- What else could iter(5) mean?

James_Althoff at i2.com James_Althoff at i2.com
Tue Mar 5 15:17:28 EST 2002


[Huaiyu]
> I do not think that there is any conceptual difficulty to
> explain that
>        for i in n    ==>    for i in iter(n)
>        iter(3)       ==>    xrange(3)
>
> which is all that is in the proposal.  This is elegant and
> easily explainable even to a newbie.  I actually like this
> quite a lot.

And I, in turn, am quite fond of this part of your message <wink>.

> However, I still think the potential for unintended effects
> (bugs) is too big when a one-element sequence behaves substantially
> different from the element itself.  Imagine if Python had a character
> type and that
>         "abc" == ['a', 'b', 'c'] == 'd' != "d"
>
> Even though it can be explained, what utter confusion it would
> result in actual use!

Sorry, I'm a little confused by this one.  Example: "for x in ['spam']" is
not the same as "for x in 'spam'".  Isn't that a case "when a one-element
sequence behaves substantially different from the element itself" in this
context?

> Here's another example of a similar nature.  In Matlab there is a
> fuction sum(x) that would return sum of elements of x, if x is a
> (row or column) vector, or a row vector of columnwise sums of x,
> if x is a matrix.  This is very handy when used in isolation, but
> a pain in programs, because the behavior on (1xm) matrix has a
> different semantics.  Suppose x is a (5x4) matrix.  Then (using
> Python syntax instead of Matlab's)
>         sum(a[0:n:, :])
> would give a (nx4) matrix for n in 5, 4, 3, 2, but for n==1,
> it gives a scalar which is the sum of x as a row vector.

Not really following that example either.  Sorry.

Jim





More information about the Python-list mailing list