1

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)
user2066880
  • 4,555
  • 9
  • 35
  • 60

1 Answers1

3

Try this,

x = int(x) if x else None

or

x = int(x) if x is not None else None
Avinash Raj
  • 166,785
  • 24
  • 204
  • 249