why not "name = value if condition"?

Hello, 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 ... Denis PS: 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

On Tue, Apr 7, 2009 at 9:03 AM, spir <denis.spir@free.fr> wrote:
Hello,
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 ...
Denis
PS: 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).
You do realize that "if condition: name = value" is a valid one-liner, right ? if/else on the other hand is a two-liner at least. George

On Tue, 7 Apr 2009 11:03:41 pm spir wrote:
Hello,
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 for the same reason that: name = is invalid -- you need to have a value to bind to the name. if condition: name = value is different. If condition is false, the branch "name = value" isn't taken at all. The entire block isn't an expression, so it doesn't have, or need, a value.
[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
Maybe so, but this can just as easily be written: if fileName is None: fileName = title+".py" And if you're concerned about this being a two-liner (perhaps because the Enter key on your keyboard is broken *wink*) you can write it as a one-liner: if fileName is None: fileName = title+".py" -- Steven D'Aprano

Le Tue, 7 Apr 2009 23:38:21 +1000, Steven D'Aprano <steve@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. Denis ------ la vita e estrany

As an aside only, it would be pretty reasonable IMO to have name = value if condition and set name to None otherwise But I'm -1 on the idea as I think it's a bit redundant. Cheers, -T On Wed, Apr 8, 2009 at 12:55 AM, spir <denis.spir@free.fr> wrote:
Le Tue, 7 Apr 2009 23:38:21 +1000, Steven D'Aprano <steve@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.
Denis ------ la vita e estrany _______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas
-- -------------------------------------------------- Tennessee Leeuwenburg http://myownhat.blogspot.com/ "Don't believe everything you think"

spir wrote:
Hello,
What's the reason why name = value if condition is invalid?
Sure, one can write if condition: name = value
The 'a if b else c' construct is an expression. The transformation you want can't be implemented by treating it as an expression, since you want the 'if' to apply to the assignment, not just the RHS.
but the rationale in favour of, or against, a one-liner shortcut is the same as for the ternary case (with else).
No, it's not. The ternary case can be used as an expression anywhere, often saving an assignment to an intermediate variable. And when used with an assignment, it avoids repeating the LHS, i.e. a = b if c else d as opposed to if b: a = c else: a = d Your proposed shortcut doesn't save anything except a small amount of whitespace, so the justification is much weaker. -- Greg
participants (5)
-
George Sakkis
-
Greg Ewing
-
spir
-
Steven D'Aprano
-
Tennessee Leeuwenburg