def hillupillu():
a= None
b="lillestalle"
return a,b
if i and j in hillupillu(): #how can i check if i or j are empty? this is not doing it of course;-P
print i,j
r = hillupillu()
if len(r) != 2: # maybe check whether it's a tuple as well with isinstance(r, tuple)
print("Hey, I was expecting two values!")
i, j = r
从函数接收值后:
可以使用
is
运算符检查值是否为None
:你也可以测试这个值的真值:
如果你的意思是你不能预测某个函数的返回值的数目,那么
如果函数不返回正好两个值,则将引发
ValueError
。你可以用通常的try
结构来捕捉它:这遵循了常见的Python习惯用法“请求原谅,而不是允许”。如果
hillupillu
可能会引发ValueError
本身,则需要执行显式检查:如果您想检查返回值中的
None
,那么请检查if
-子句中的None in (i, j)
。Functions in Python always return a single value。特别是它们可以返回一个元组。
如果不知道一个元组中有多少值,可以检查其长度:
相关问题 更多 >
编程相关推荐