Waffling (was Re: single-line terinary operators considered harmful)
Steven Taschuk
staschuk at telusplanet.net
Wed Mar 5 14:31:34 EST 2003
Quoth Stephen Horne:
[...]
> When I was saying that optionalness of the semicolon makes the
> difference between separation and termination, [...]
So, in Python:
[1, 2, 3,]
[1, 2, 3]
Is the comma a terminator or a separator?
[...]
> A fairly common would be something like...
>
> void strcpy_alike (char *p_Dest, char *p_Src)
> {
> while ((*p_Dest++ = *p_Src++) != 0);
> }
>
> <loud retching noises>
A bit off-topic, but imho a minor style change improves this
dramatically:
while ((*p_Dest++ = *p_Src++) != 0)
;
With the semicolon all lonesome on the next line, it is very
noticeable; it shouts "this statement intentionally left blank".
Now and then I use 'pass' in Python in a similar way:
def nop():
"""Do nothing."""
pass # not required by the syntax
(Also useful in empty class definitions that have docstrings.)
[...]
> I've seen languages where no operators could shortcircuit, and I've
> seen plenty of languages where functions use lazy evaluation. In
> Python, I'd be surprised if the expression '0 * x' was shortcircuited
> even though it uses an operator and the right-hand-side is redundant.
[...]
It's not redundant at all: 0*1 and 0*[] yield objects of different
types; if x is an instance of a user-defined vector class, the
multiplication should return the zero vector, not 0; and so forth.
The second operand must be evaluated to achieve this polymorphism.
--
Steven Taschuk staschuk at telusplanet.net
Receive them ignorant; dispatch them confused. (Weschler's Teaching Motto)
More information about the Python-list
mailing list