我有一個遞歸調用結束時我的一段代碼如下:力Python來返回0,或無時返回0
if (condition):
# Some code here
else:
return function_call(some_parameters) or function_call(some_parameters)
,它可以評估到
return None or 0
在那裏將返回0(一個整數,如預期) 或
return 0 or None
在那裏它將返回None(預期0)
我的問題是,是否有可能使Python在上面的情況下返回0(作爲INTEGER)?
這裏是代表場景
$ cat test.py
#!/usr/bin/python3
def test_return():
return 0 or None
def test_return2():
return None or 0
def test_return3():
return '0' or None #Equivalent of `return str(0) or None`
print(test_return())
print(test_return2())
print(test_return3())
$ ./test.py
None
0
0
注意一些代碼:0應爲整數返回。
你爲什麼這樣做呢?只需鍵入0,那麼它將始終返回0. – Alperen
其他可能的返回結果是什麼? – Fejs
...'x = fun(); y = f();'然後'如果x不是None else y就返回x? –