Safe casting in python

后端 未结 5 1252
Happy的楠姐
Happy的楠姐 2020-12-31 03:36

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=\"fifte         


        
5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-31 04:06

    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
    

提交回复
热议问题