python - Is it okay to put error-prone code in an OR statement? -


this in python, can applied language.

a = none if (not a) or (a+3==5):     print("is okay?") 

is programming practice have a+3 in if statement, though cause error if a none?

i'm relying on or stop before error, seems bad idea me in case a else doesn't support addition, or or statement (in other language) looks @ both values.

what best way program this?

yes can depend on short-circuiting perform error handling , validation.

however in case, logic wrong, need and perform error handling

if , (a+3==5): 

alternatively type checking

if isinstance(a, int) , (a+3==5): 

again short-circuit before reading + operation


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -