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

Huaiyu Zhu huaiyu at gauss.almadan.ibm.com
Tue Mar 5 22:23:42 EST 2002


On Tue, 5 Mar 2002 12:17:28 -0800, James_Althoff at i2.com
<James_Althoff at i2.com> wrote: 
>> 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?

OK, I used the wrong words here.  I meant to say that an element of a
sequence being functionally equivalent to another sequence of the same type
but different.  This excludes cases like "d"[0]=="d".  But if "d" produces
the same effect as "abc" under some circumstances, it would be very
confusing.  Likewise if "spam" produces the same effect as ["spam", "egg"].

The reason is not philosophical.  It's just that human minds are easy to
confuse on these matters, so we want exceptions in all such cases:

"spam".append("eggs")
x, y, z = "d"
x, y, z = 3
[f(x) for x in (4 in 3) in (2 in 3) if x in (x+1 in x*2)] # just for fun. :-)

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

OK, I have to admit that this does not really apply.  Syntacitically (6,) is
quite different from (6).  So it is not like the Matlab situation where a
1xn matrix is treated as a row vector.  So you could argue that they are not
neighbors and there is no discontinuity.  However, in terms of human
behavior (typo frequency) they are indeed close to each other.  This is
similar to the relation between "=" and "==".

On the other hand, in the special case where 0 and 1 are used as logical
values your proposal is in fact consistent.  This is a plus.


Huaiyu



More information about the Python-list mailing list