I have a situation where I would like to conditionally slice a string from the reported position of an '@' symbol; the condition being: slice the string if the '@' is there, else leave it untouched. I thought up two ways, one using a function, the other using an inline conditional expression. Which method is the most Pythonic?
Using a function
>>> def slice_from_at(inp):
... res = inp.find('@')
... if res == -1:
... return None
... else:
... return res
>>> c = 'agent_address@agent_address'
>>> c[:slice_from_at(c)]
... 'agent_address'
Using an inline conditional expression
>>> c = 'agent_address@agent_address'
>>> c[:None if c.find('@') == -1 else c.find('@')]
... 'agent_address'
Although using the inline conditional expression is more terse and, some may argue more economical - is the function method is more Pythonic because it more readable?