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

Steven Taschuk staschuk at telusplanet.net
Thu Mar 6 03:12:08 EST 2003


Quoth Stephen Horne:
> On Wed, 5 Mar 2003 12:31:34 -0700, Steven Taschuk
> <staschuk at telusplanet.net> wrote:
  [...]
> >So, in Python:
> >	[1, 2, 3,]
> >	[1, 2, 3]
> >Is the comma a terminator or a separator?
> 
> Same thing. With either semicolons for (at least Pascal) statements
> and commas in the above lists, the most pragmatic answer is 'who
> cares'.  [...]

Why, surely this is an important theoretical point!  How can we
say we know Python if we don't even know whether its commas are
separators or terminators?!

After donning my holy war hat (to banish the thought that these
terms are not a disjoint partition of the marks in question), I
see they're definitely terminators in tuples, but probably
separators elsewhere.

> [...] Of course I tend to see commas as separators and find your top
> example strange, and of course I might well see semicolons the same
> way if I hadn't used them as terminators in several other languages.

Did you see semicolons thus when you started programming?  They're
separators in all the natural languages which use them, afaik.

I agree a final comma is strange in a one-line list such as
[1, 2, 3,], but as pointed out in another thread a little while
back, it's very handy when the list is long:
    spam = [
        a_big_long_expression,
        another_big_long_expression,
        a_third_big_long_expression,
        ]
The superfluous final comma makes this list easier to edit; no
puncutation adjustments needed when moving items to and fro.  Good
style, imho.

> >	while ((*p_Dest++ = *p_Src++) != 0)
> >		;
> 
> I'm not sure. With modern compilers, using side effects like this is
> IMO inherently bad - without the side-effects the loop has no purpose.
> So while your right about the 'this line intentionally left blank'
> effect, I still wouldn't use it myself. This style of code had value
> when C was created (before decent optimisers had been developed), but
> IMO it's had its day.

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

-- 
Steven Taschuk                                                   w_w
staschuk at telusplanet.net                                      ,-= U
                                                               1 1





More information about the Python-list mailing list