呼び出される関数の内部から関数が呼び出される出力引数の数をチェックする方法は Python にありますか?
例えば:
a,b = Fun() #-> number of output arguments would be 2
a,b,c = Fun() #-> number of output arguments would be 3
matlab では、これはnargout を使用して行われます。これを行う「通常の方法」は、不要な値を _ 変数にアンパックすることです。
def f():
return 1, 2, 3
_, _, x = f()
私が達成しようとしていることは単純です。いくつかの引数または2つのオブジェクトで呼び出された場合は単一のオブジェクトを返す関数があります。
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.