The syntax:
c = a if condition else b
is a proxy for:
if condition:
c = a
else:
c = b
And MUST have both branches of the condition (as detailed in the grammar description from the official documentation and further discussed in the corresponding PEP), which is missing in your code. More discussion on the if
/else
ternary operator in Python here.
The same holds true for return
:
return a if condition else b
is equivalent to:
if condition:
return a
else:
return b
In your code, if you do not have instructions after that condition, you could use:
return None if a is None else None
which turns out to be a pretty useless statement, as can be replaced with:
return None
or even just omitted altogether.
Finally:
if a is None:
return None
is already the Pythonic version of the code.
Acceptable alternatives include:
if a is None:
return
if a is None: return None
if a in None: return
PEP8 Style Guide for Python Code suggests avoiding putting multiple statements on the same line:
Compound statements (multiple statements on the same line) are generally discouraged.
however, is open to small-body one-liners:
sometimes it's okay to put an if/for/while with a small body on the same line