最佳答案
当 my_var
可以为无时,使用以下格式是不是不好的做法?
if my_var and 'something' in my_var:
#do something
问题是,如果 my _ var 为 Nothing,'something' in my_var
将抛出 TypeError。
或者我应该用:
if my_var:
if 'something' in my_var:
#do something
或者
try:
if 'something' in my_var:
#do something
except TypeError:
pass
换句话说,上面哪个是 Python 中的最佳实践(如果有的话) ?
我们欢迎其他选择!