expression IF (was: Why does python not have a mechanism for data hiding?)

John Nagle nagle at animats.com
Sat Jun 7 20:18:30 CEST 2008


BJörn Lindqvist wrote:
> On Wed, Jun 4, 2008 at 2:02 PM, Antoon Pardon <apardon at forel.vub.ac.be> wrote:
>> Now of course noone would defend such a limitation on the grounds
>> that one doesn't need the general case and that the general case
>> will only save you some vertical space.
>>
>> But when it came to the ternary operator that was exactly the
>> argument used, to defend the lack of it.
> 
> As far as I remember, the primary motivation was developers experience
> with the ternary operator in other languages, especially C, where it
> was found to hurt readability. At least in my experience, it is much
> much more common to see the ternary operator making code more
> obfuscated than easing readability. Time will tell if Python's if-else
> expression will be abused in the same way.

     Expression IF statements are most useful in situations where you can't
have multiple statements.  In C, that was usually in macros, or occasionally
in a function call:

	printf("Boolean value: %s\n", b ? "True" : "False");

was useful, for example.  The syntax was probably a bad choice.
The classic expression IF (an ALGOL extension) was

	x := IF b THEN 1 ELSE 0;

Surrounded with parentheses, that form could be used in any expression
context.

     Given Python's approach to indentation, conditionals
within expressions (and iteration; one could have FOR / YIELD) don't
fit the syntax.  That's why lambda expressions in Python are so limited.
Python already has local functions; you can write:

def foo(n) :
	def bar(m) :
		print "In BAR",n, m
	bar(n+1)

which also allows you to do anything you can do with a lambda expression.
You can have control structure, and it reads like ordinary Python.
So there's no real need for an expression IF operator.

				John Nagle




More information about the Python-list mailing list