python有沒有一種方法來檢查函數被調用的函數內部調用的輸出參數的數量?Python的檢查一個函數被調用的輸出參數的數量
例如:
a,b = Fun() #-> number of output arguments would be 2
a,b,c = Fun() #-> number of output arguments would be 3
在MATLAB中,這將使用nargout 做我知道,這樣做的"normal way"是解壓不需要的值到_變量:
def f():
return 1, 2, 3
_, _, x = f()
什麼我試圖完成很簡單。我有一個會返回一個對象,如果調用一些參數或兩個對象,否則一個功能:
def f(a,b=None):
if b is None:
return 1
else:
return 1,2
但我想迫使元組拆包不會發生,而是強制錯誤,例如:
x = f(a) #-> Fine
x,y = f(a,b) #-> Fine
x,y = f(a) #-> Will throw native error: ValueError: need more than Foo values to unpack
x = f(a,b) #-> Want to force this to throw an error and not default to the situation where x will be a tuple.
您可能希望通過[蟒蛇思路(HTTP檔案脫脂:// mail.python.org/mailman/listinfo/python-ideas)。有人建議添加一個獨立於迭代器協議的「解包協議」,這會使這樣的事情幾乎微不足道。 IIRC,這個想法被擊落了,但是一路上有人提出了一些「今天Python中的黑客實現」。 – abarnert 2013-05-10 19:06:03