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

spir denis.spir at free.fr
Tue Apr 7 16:55:10 CEST 2009

Le Tue, 7 Apr 2009 23:38:21 +1000,
Steven D'Aprano <steve at pearwood.info> s'exprima ainsi:

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

> Because "value if condition else other" is an expression, not a 
> statement, and thus *must* have a value.
>     name = value if condition
> only has a value sometimes, and so is invalid [...]

Yes, thank you! That's the explaination I was looking for.
I meant to use the above formulation for updating already defined variables. Indeed, python does not make the difference I have in mind ;-).

As a side note,
   if condition: name = value
(where 'name' is already registered) is the only case when I do not indent a block, for the reason that it means for me the same as the above invalid one-liner.

la vita e estrany

More information about the Python-ideas mailing list