Python: try statement in a single line

前端 未结 13 1632
囚心锁ツ
囚心锁ツ 2020-12-01 03:47

Is there a way in python to turn a try/except into a single line?

something like...

b = \'some variable\'
a = c | b #try statement goes here
<         


        
13条回答
  •  无人及你
    2020-12-01 04:44

    Here is a simpler version of the answer provided by @surendra_ben

    a = "apple"
    ​
    try: a.something_that_definitely_doesnt_exist
    except: print("nope")
    
    ...
    
    nope
    

提交回复
热议问题