Waffling (was Re: single-line terinary operators considered harmful)

Stephen Horne intentionally at blank.co.uk
Thu Mar 6 05:34:56 EST 2003


On Thu, 6 Mar 2003 01:12:08 -0700, Steven Taschuk
<staschuk at telusplanet.net> wrote:

>The superfluous final comma makes this list easier to edit; no
>puncutation adjustments needed when moving items to and fro.  Good
>style, imho.

I agree - occasionally, I've run the equivalant of one of those
through an editiors sort and forgot that the (previously) last line
didn't have a comma. Annoying.

>Hm.  How about a case like this?
>
>    def consume(iterator, sentinel):
>        """Consume elements from iterator up to and including
>        first occurrence of sentinel, or end of stream.
>        """
>        try:
>            while it.next() != sentinel:
>                pass
>        except StopIteration:
>            pass
>
>(Could be useful for error recovery in parsers, for example.)

True - there is little point getting all extremist even over
side-effects, and I write something like this from time to time - as
well as the short-circuiting logic used to run a sequence up to the
first error (each function having an error-indicating return value and
side-effects to do the real job).

Mostly, the name is a big help.

I tend to dislike '++' in C more than perhaps I should (and more than
the 'next' function above) because the C standard does not guarentee
when the postincrement is done - it could be immediately when the
value is evaluated, it could be after the whole expression containing
it is fully evaluated, or it could be any time in between. Which means
that if you refer to the same variable twice in the expression, and
the first has a postincrement, you simply don't know what value the
second reference will give. This is quite easy to do by accident, and
many programmers don't even know the ambiguity exists.

That doesn't happen with a function, and can't happen at all in
Python.

-- 
steve at ninereeds dot fsnet dot co dot uk




More information about the Python-list mailing list