[Python-ideas] why not "name = value if condition"?

spir denis.spir at free.fr
Tue Apr 7 15:03:41 CEST 2009


What's the reason why
   name = value if condition
is invalid? Meaning: there _must_ be an else clause.

[I imagine this has been discussed and refused consciously, but I couldn't find it in PEP308, nore in archives.]

It would be practicle in many situations, e.g. to give default values to parameters:

def writeFile(text, title, format=STANDARD, fileName=None):
   fileName = title+".py" if fileName is None


Sure, one can write
   if condition:
      name = value
but the rationale in favour of, or against, a one-liner shortcut is the same as for the ternary case (with else).

One can also write:
   name = value if condition else name
but... <no comment>.

I find the present situation a bit frustrating, like sitting between 2 chairs.

la vita e estrany

More information about the Python-ideas mailing list