当测试变量是否具有值时,是否有一个基本原理来决定使用哪个try或if结构?
例如,有一个函数要么返回一个列表,要么不返回值。我想在处理之前检查一下结果。下面哪个更可取,为什么?
result = function();
if (result):
for r in result:
#process items
or
result = function();
try:
for r in result:
# Process items
except TypeError:
pass;
相关讨论:
检查Python中的成员是否存在