我有一个返回元组或无的函数。来电者应该如何处理这种情况?
def nontest():
return None
x,y = nontest()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not iterable
最佳答案
EAFP :
try:
x,y = nontest()
except TypeError:
# do the None-thing here or pass
或者没有try-except:
res = nontest()
if res is None:
....
else:
x, y = res
关于python - 如何处理从 python 函数返回的空(无)元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16533672/