I have a variable x
that I want to cast to int or return None if it is None.
Is there a shorthand way of doing the following:
if x:
x = int(x)
I have a variable x
that I want to cast to int or return None if it is None.
Is there a shorthand way of doing the following:
if x:
x = int(x)
Try this,
x = int(x) if x else None
or
x = int(x) if x is not None else None