Too much code - slicing

Seebs usenet-nospam at seebs.net
Sun Sep 19 09:31:18 CEST 2010


On 2010-09-19, Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> wrote:
> Define "unbalanced".

I'm not sure that's the word I'd use.  I'm not even sure what it would mean
here.

> Putting aside the over-use of punctuation, The C syntax feels unbalanced 
> to me. You have:

> condition IF true-clause ELSE false-clause

> so both clauses follow the test, that is, they're on the same side: ?--

Yes.

Just like:
	if condition:
	    foo
	else:
	    bar

The condition is the primary, the clauses are secondary to it.

So I like that form because it matches what I'd write if I were writing
things out more verbosely for some reason.

> But the Python syntax looks balanced to me:

> true-clause IF condition ELSE false-clause

> which is not only plain English, but perfectly balanced, with a clause on 
> either side of the test: -?-

It may be balanced, but it requires you to reevaluate what you're reading
after you've already read something that seemed to have a clear meaning.

Basically, think of what happens as I read each symbol:
	
	x = x + 1 if condition else x - 1

Up through the '1', I have a perfectly ordinary assignment of a value.
The, suddenly, it retroactively turns out that I have misunderstood
everything I've been reading.  I am actually reading a conditional, and
the things I've been seeing which looked like they were definitely
part of the flow of evaluation may in fact be completely skipped.

It's even more confusing if I'm familiar with the postfix-if as seen
in, say, perl or ruby.

> Python's ternary-if puts the emphasis on the true-clause, while C's 
> ternary-if puts the emphasis on the test. I'm not convinced that this is 
> necessarily a better choice than Python's. It's a *valid* choice, but 
> better? I don't think so, but I accept that at least partially boils down 
> to subjective factors.

I would usually think it better, just because most often, the *fact*
of there being a test is the first thing you have to know about the
expression to make sense of it.

If I am given the right framework first, and the information to fill into
it second, I don't have to throw away parsing I'd already done.  If I'm
given information, then later retroactively told to move it into a slot
in a framework I didn't even know I was going to need, I have to do a lot
of reworking.

Consider the following lovely hypothetical syntax:

		foo
		bar
		baz
	if condition else:
		blah
		blah
		blah

And there, at least you have some cue that you're about to see something
happen.

-s
-- 
Copyright 2010, all wrongs reversed.  Peter Seebach / usenet-nospam at seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.



More information about the Python-list mailing list