1

Am I right in thinking I can't put an if-statement and the corresponding else-statement on one line in Python?

NB:

variable = value1 if condition else value2

is NOT two statements. It's one statement which can take the value of one of two expressions.

I want to do something like

if condition a=value1 else b=value2

Am I right in thinking this requires a full if-else in Python? Like

if condition:
    a=value1
else:
    b=value2

Note as I already noted above (this is inherent to the definition of what is a statement and what is an expression, with which every programmer should be familiar), because I am I assigning two different variables, the above is quite different to

if condition:
    a=value1
else:
    a=value2

where I could of course simply use a conditional expression

a = value1 if condition else value2

Therefore my question is not a duplicate of Does Python have a ternary conditional operator?

Community
  • 1
  • 1
Louise
  • 703
  • 6
  • 19
  • 1
    Out of interest, why would you want to do this? – Talvalin Jun 12 '14 at 21:20
  • Beautiful code. Dozens of short statements like this would look nicer on one line each, than on four lines each! – Louise Jun 12 '14 at 21:21
  • 5
    If you have dozens of related but distinct variables, I think your code needs refactoring. – chepner Jun 12 '14 at 21:24
  • @Talvalin It's not a duplicate! That question is referring to expressions, not statements. This question is very different. – Louise Jun 12 '14 at 21:27
  • @chepner Thank you for the advice, but I did not write the functions which I have to pass these variables to. In a perfect world we could refactor everything :) – Louise Jun 12 '14 at 21:28
  • 3
    "Beautiful code", in my mind, does not include single-line hacks for their own sake. [Readability counts!](http://legacy.python.org/dev/peps/pep-0020/) – Henry Keiter Jun 12 '14 at 21:28
  • @HenryKeiter This isn't a hack, dozens of other languages allow this syntax. – Louise Jun 12 '14 at 21:28
  • @HenryKeiter For example in C, if (c) {a=b;} else {b=c;} is very readable. – Louise Jun 12 '14 at 21:29
  • @Louise Dozens of other languages are more-or-less whitespace agnostic, so in-lining arbitrary code is trivial. In Python, this is a hack. – Henry Keiter Jun 12 '14 at 21:31
  • Perhaps if you edited your question to include a clear explanation of what you want to do and why we could give better help? – Jack Aidley Jun 12 '14 at 21:33
  • Don't treat `a` and `b` differently and your problem goes away – Daenyth Jun 12 '14 at 21:35
  • Just out of curiosity in Ruby you can simply achieve this with ternary operator: `condition ? a=value1 : b=value2` . – David Unric Jun 12 '14 at 21:37
  • The question space is the space for questions. The comment space is the place for comments. Please adhere to this. – Veedrac Jun 12 '14 at 21:53
  • 1
    The linked question is not a duplicate. – Jack Aidley Jun 13 '14 at 05:25
  • Look up the syntax at https://docs.python.org/3/reference/compound_stmts.html#the-if-statement and you will see that the statement parts have to have a NEWLINE token. But it is not a duplicate! – Bull Jun 13 '14 at 06:20

3 Answers3

2
my_items = {}
my_items["a" if condition else "b"] = value

would be better I would think ...

but you could do

globals()["a" if condition else "b"] = value

which would do exactly what you are asking (although in general this is frowned upon)

if you wanted to get really gross you could do

eval("a=value1" if condition else "b=value2") 

but I would never ever recommend actually doing something like that

to be honest I have a hard time thinking of any valid use-case where you would actually want

if condition:
  a = value1
else:
  b = value2
Joran Beasley
  • 93,863
  • 11
  • 131
  • 160
  • Thanks! :) This still isn't two separate statements though, what if I want the value to be conditional too? Sorry, I gave an overly simplistic example. – Louise Jun 12 '14 at 21:25
2

Python requires you to put your if and your else on different lines because failure to do so makes your code difficult to read and makes it easy to write unpleasantly unreadable code.

It is unlikely your code will actually be better with this feature.

Instead consider using a function to give a more meaningful aspect to your code, by returning a tuple you can alter both a and b at the same time.

Jack Aidley
  • 17,045
  • 6
  • 35
  • 66
1

Wouldn't actually recommend it, but you could also use tuple unpacking:

a,b = (value1 if condition else a), (value2 if not condition else b)
chrisb
  • 39,034
  • 8
  • 55
  • 56
  • 2
    or shorter: `a,b = value1, b if condition else a, value2` – tobias_k Jun 12 '14 at 21:30
  • Thanks! :) I think this might confuse people though. – Louise Jun 12 '14 at 21:31
  • 1
    @Louise If you don't want to confuse people, put those statements on separate lines, at least when using Python. Python is whitespace-sensitive for a reason: To improve readability. – tobias_k Jun 12 '14 at 21:32