I have this code here:
import re
def get_attr(str, attr):
m = re.search(attr + r'=(\w+)', str)
return None if not m else m.group(1)
str = 'type=greeting hello=world'
print get_attr(str, 'type')
print get_attr(str, 'hello')
print get_attr(str, 'attr')
Which works, but I don't particularly like this line:
return None if not m else m.group(1)
In my opinion, this would look cleaner if we could use the ternary operator:
return (m ? m.group(1) : None)
But this, of course, is not. What are you offering?
source
share