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,825
  • 9
  • 38
  • 64

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
  • 172,303
  • 28
  • 230
  • 274