FW: [Python-Dev] conditional expressions?

Martijn Faassen m.faassen at vet.uu.nl
Mon Oct 15 20:12:41 EDT 2001


Brian Quinlan <BrianQ at activestate.com> wrote:
> Tim wrote:
>> If you don't use parens, they're equivalent to
>>
>>     ]*** ) 3fo?rkl

> Tim, please stop using idiomatic Perl in your examples - it's hard for
> some of us to follow.

My initial response is that conditional expressions don't look pretty..
I don't know what I don't like about them; even C's variety seems nicer to
me right now, probably because I'm used to those.

I think it's a combination of the reuse of the word 'if' (which in my
mind triggers "this is a statement") and the long lines resulting from
this usage. Three keywords and possibly some parens, and all you have
is just *part* of an expression! I think this is only matched by
list comprehensions.

The difference is that list comprehensions are commonly used in isolation 
(in an assignment), while conditional expressions aren't worth that much
if used outside more complicated expressions. 

I think list comprehensions win out on various grounds, even though
they're a three statement expression as well.. This is all pretty
subjective, so some theories as to why..

 
for x in (if foobar(x) then range(100) else my_own_function(77)):
   pass

for x in [foobar(x) for x in range(100) if my_own_function(x)]:
   pass
 
  * list comprehensions are signalled by the fairly clear [ and ]. 
    Conditional expressions use the already fairly strongly 
    overloaded ( and ). Of course I guess they wouldn't really 
    overload them further, but.. 

  * Perhaps one can lose the parens in the conditional expression.
    Can one? What is the rule? Is the rule easy to remember? Of course
    I think it'd look less readable still without the parens...

  * list comprehension don't *have* to use the 'if' part, while
    conditional expressions will have to use the else part. 
    (not orthogonal with the optionality of 'else' in the 'if' statement,
     either, but that's a minor issue)

  * The list comprehension always generates a list,
    which limits the ways in which you can use them in obscure ways.
    (if one did not set out deliberately to do so, that is) 
    A conditional expression could evaluate to anything, so can be used
    in more places more obscurely.

  * in a list comprehension, all parts are fairly clearly related to each
     other. For instance, in the variable, x is post-processed in the
     first part, iterated through in the 'for x' part, and generated
     by the range(100) part. The last part again does somekind of check
     on x. In a conditional expression this relationship can I think
     frequently be absent, which may make it harder to grok as a single
     thing.

...

But these are just vague responses. Perhaps I'll get used to them
pretty quickly. What do others think? Excuse me if I missed somekind
of long thread about this and I'm repeating the often stated. :) 

Regards,

Martijn
-- 
History of the 20th Century: WW1, WW2, WW3?
No, WWW -- Could we be going in the right direction?



More information about the Python-list mailing list