Flavius
Flavius

Reputation: 13816

nested if-else as one-liners

How to write the following nested if-else

        if 'parent' in commit:
            if commit['parent'] == cid-1:
                parent = '    '
            else:
                parent = commit['parent']
        else:
            parent = '<no parent>'

as an one-liner? The outer if-else could be written as

parent = commit['parent'] if 'parent' in commit else '<no parent>'

Please describe how to write it syntactically for complex expressions, generally, not only for this case. But do use this example as a showcase.

Upvotes: 1

Views: 204

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121256

Yes, your code will work as a one-liner if you combine the 'inner' expression with the outer:

parent = ('    ' if comment['parent'] == cid-1 else commit['parent']) if 'parent' in commit else '<no parent>'

The if of the outer expression is evaluated first; see the compiled bytecode of the above 'beauty':

>>> import dis
>>> def foo():
...     parent = ('    ' if comment['parent'] == cid-1 else commit['parent']) if 'parent' in commit else '<no parent>'
... 
>>> dis.dis(foo)
  2           0 LOAD_CONST               1 ('parent') 
              3 LOAD_GLOBAL              0 (commit) 
              6 COMPARE_OP               6 (in) 
              9 POP_JUMP_IF_FALSE       48 
             12 LOAD_GLOBAL              1 (comment) 
             15 LOAD_CONST               1 ('parent') 
             18 BINARY_SUBSCR        
             19 LOAD_GLOBAL              2 (cid) 
             22 LOAD_CONST               2 (1) 
             25 BINARY_SUBTRACT      
             26 COMPARE_OP               2 (==) 
             29 POP_JUMP_IF_FALSE       38 
             32 LOAD_CONST               3 ('    ') 
             35 JUMP_ABSOLUTE           51 
        >>   38 LOAD_GLOBAL              0 (commit) 
             41 LOAD_CONST               1 ('parent') 
             44 BINARY_SUBSCR        
             45 JUMP_FORWARD             3 (to 51) 
        >>   48 LOAD_CONST               4 ('<no parent>') 
        >>   51 STORE_FAST               0 (parent) 
             54 LOAD_CONST               0 (None) 
             57 RETURN_VALUE         

So, if 'parent' is not found in commit, it'll jump to instruction 48 and load '<no parent>' and not look at commit['parent'] at all.

Not sure how readable the line is, I find your explicit version much easier to understand.

Upvotes: 3

Related Questions