Is there a way in python to check for the number of output arguments a function is called with from inside of the function being called?
For example:
a,b = Fun() #-> number of output arguments would be 2
a,b,c = Fun() #-> number of output arguments would be 3
In matlab this would be done using nargout I know that the "normal way" of doing it is to unpack the unwanted values to the _ variable:
def f():
return 1, 2, 3
_, _, x = f()
What I am trying to accomplish is simple. I have a function that will return a single object if called with some arguments or two objects otherwise:
def f(a,b=None):
if b is None:
return 1
else:
return 1,2
But I would like to force the tuple unpacking not to happen and force an error, for example:
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.