3

A simple question. First off, I've noticed in Python that I can make things more concise by changing short statements like this:

if some_condition:
    do_something()

To this:

if some_condition: do_something()

This change, of course, only works if the code inside the if statement consists of only one line.

However if there is more than one nested "construct" (I'm referring to things like an if-else, for, while, or try-except statement) then I get a syntax error. For example, I can't change this:

if some_condition:
    if other_condition:
        do_something()

To this:

if some_condition: if other_condition: do_something()

Or even this:

if some_condition: if other_condition:
    do_something()

But this does work:

if some_condition:
    if other_condition: do_something()

My guess is that the reason for this is that having two constructs on one line like that creates some kind of ambiguity. I would like to know if there is some way I can still put two statements on a line but have it work. For example, maybe something similar to this:

if some_condition: (if other_condition: do_something())

That, of course, doesn't work. However, hopefully, it makes it a little more clear what exactly I'm trying to do here. Any ideas would be appreciated other than "You shouldn't do this."

Before I get a rush of all you purists out there coming in and preaching how this isn't Pythonic or whatever, YES, I know that this isn't the best way to write code in Python. Consider it a research question. I just want to know if what I'm looking for is possible.

Lin Du
  • 88,126
  • 95
  • 281
  • 483
Aaron Beaudoin
  • 1,107
  • 1
  • 10
  • 23
  • Possible duplicate of [How to put multiple statements in one line?](https://stackoverflow.com/questions/6167127/how-to-put-multiple-statements-in-one-line) – Alec Apr 25 '19 at 02:03
  • You know you could link the conditions with `and`, right? – user2357112 Apr 25 '19 at 02:21

4 Answers4

1

It's impossible to put multiple colons on one line.

Regardless, PEP8 recommends always following a colon with a new line. In most cases, it's best to follow this guideline.

As @Suven Pandey notes, if statements can be nested on one line using nested ternary operators, but please don't use more than one on a single line. At that point, the code is supremely ugly and unreadable.

Alec
  • 8,529
  • 8
  • 37
  • 63
0

In some cases you can do:

a = 7
b = 4
print(a) if a == 7 else 3 if b ==4 else 0 #print a 
print(a) if a == 27 else 3 if b ==4 else 0 #print 3
print(a) if a == 27 else 3 if b ==44 else 0 #print 0

This works like print(a) if a == 7 else (3 if b ==4) #if a != 7 but b = 4 else 0 #both false but its confusing and usually a bad idea and violates the zen of python.

Nevus
  • 1,307
  • 1
  • 9
  • 21
0

You can use the exec function to combine nested constructs on a single line as long as you provide the correct newline and tab combinations within the string to be executed.

exec ('a=7\nif a>3:\n\tfor x in range(a):\n\t\tif x != 5:\n\t\t\tprint("Wow!")')
bashBedlam
  • 1,402
  • 1
  • 7
  • 11
0

The question was how to get around it, but I wondered why multiple colons on the same line are verboten. The O.P. guessed ambiguity, and the docs mention one:

if x: if y: foo() else: bar()

could mean

if x: 
    if y: 
        foo()
else: 
    bar()

or

if x: 
    if y: 
        foo()
    else: 
        bar()
Bob Stein
  • 16,271
  • 10
  • 88
  • 101