Is it possible to use Python inline elif?

'Hello ' + ('there' if name is None else name) 

Is the equivalent

 msg = 'Hello ' if name is None: msg += 'there' else: msg += name 

Which is equivalent to this:

 msg = 'Hello ' if name is None: msg += 'there' elif name == 'Mr Anderson' msg += 'Neo' else: msg += name 

EDIT: for reference, here is the code I would like to compress

 srepr = '\'Modify ' if self.register == 'p': srepr += 'Pointer' elif self.register == 'v': srepr += 'Value' else srepr += 'Unknown' srepr += ' By ' + str(self.delta) + '\'' 
+7
source share
7 answers
 msg = "Hi " + ("there" if not name else ("Neo" if name == "Anderson" else name)) 

Caution: I think it is impossible to read and will never use it.

+13
source

Do not do this.

Do this instead:

 % python -m this | sed 's/^R.*/======>&<======/' 

EDIT: For reference, here is how I would reorganize this code ...

Whenever I see elif , I think dict .

 #!/usr/bin/env python class Shrink(object): types = { 'p': 'Pointer', 'v': 'Value', } def shrink_this(self): return "'Modify %s By %s'" % ( self.types.get(self.register, 'Unknown'), self.delta) import unittest class TestShrink(unittest.TestCase): def test_p(self): s = Shrink(); s.register = 'p' s.delta = 'delta' self.assertEquals("'Modify Pointer By delta'", s.shrink_this()) def test_u(self): s = Shrink(); s.register = 'u' s.delta = 'echo' self.assertEquals("'Modify Unknown By echo'", s.shrink_this()) def test_v(self): s = Shrink(); s.register = 'v' s.delta = 'foxtrot' self.assertEquals("'Modify Value By foxtrot'", s.shrink_this()) if __name__ == '__main__': unittest.main() 

You had to add r for reference or pp for pointer-to-pointer , only types need to be changed and your code remains readable.

Readability indicators.

+2
source

Use the dictionary to do the mapping:

 srepr = "'Modify " + {"p": "Pointer", "v": "value"}.get(self.register, "Unknown") 

(by the way, instead of '\'...' you can use "'... for more clarity.

+2
source
 'Hello ' + \ ('there' if name is None else \ 'Neo' if name == 'Mr Anderson' else \ name) 

I recommend against this; if your conditions get so complicated, paste it into a function.

+1
source

First run 'pip install pyswitch'

Then:

 import pyswitch mySwitch = pyswitch.Switch() @mySwitch.case(None): def gotNone(value): return 'Hello there' @mySwitch.case('Mr. Anderson') def gotMrAnderson(value): return 'Hello Neo' @mySwitch.default def gotDefault(value): return 'Hello %s' % value msg = mySwitch.switch(None) # Returns 'Hello there' msg = mySwitch.switch('Mr. Anderson') # Returns 'Hello Neo' msg = mySwitch.switch('OoglyMoogly') # Returns 'Hello OoglyMoogly' 
0
source

You can also do this:

 msg= 'Hello ' + {name is None: 'there', name == 'Mr Anderson': 'Neo'}.get(True, name) 

Thus, if either name is None or name == 'Mr Anderson' is True , or none of them is True, then name will be used.

0
source
 msg = 'Hello ' + ( 'there' if name is None else 'Neo' if name == 'Mr Anderson' else name ) 

This is a repetition of several other answers, but with good formatting. I find this the most readable, and this is the approach I would use.

0
source

All Articles