Safe casting in python

Viewed 47497

How to do safe cast in python

In c# I can safe cast by keyword as, e.g.:

string word="15";
var x=word as int32// here I get 15

string word="fifteen";
var x=word as int32// here I get null

Has python something similar to this?

5 Answers

I do realize that this is an old post, but this might be helpful to some one.

x = int(word) if word.isdigit() else None
Related