Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nargin functionality (from Matlab) in Python

Tags:

python

matlab

I am attempting to translate these lines of code:

error(nargchk(3, 4, nargin))
if nargout > 2, error('too many output parameters'); end

I want to translate it into Python. I have yet to find an equivalent for error (although I could perhaps just throw an exception here?), nargchk and nargin. Can anybody provide any input as to how this line of code can be translated?

like image 593
A.Torres Avatar asked Sep 07 '25 01:09

A.Torres


1 Answers

There is nothing equivalent to nargin and nargchk because Python doesn't need it. You can define optional arguments in Python directly in the function definition. So for example:

def myfunc(a=1, b=3):

In this case it lets you set a and b to whatever you want, but if you don't define them it uses the default values you specified. So if you call myfunc(), it sets a to 1 and b to 3. If you call myfunc(5) it sets a to 5 and b to 3. You can also call by name, such as myfunc(b=10), which sets a to 1 and b to 10.

If you want argument to be required, just don't assign anything to them in the function definition. So in the following case, a is required while b is optional:

def myfunc(a, b=3):

If you want a user to be able to specify an arbitrary number of arguments, you can use *args (where args can be whatever variable you want. So this:

def myfunc(a=1, b=3, *args):

Will assign the first argument to a, the second argument to b, and any remaining arguments to args. You can also do this with arguments defined by name using **kwargs (again, kwargs can be any variable name). So this:

def myfunc(a=1, b=3, *args, **kwargs):

Does the same thing as the previous one, but any arguments provided by name other than a and b will go in kwargs (which is a dictionary).

So you don't need nargin and nargchk, generally you can just define the arguments you need and set default values for the optional ones. Then Python will automatically handle checking to make sure the required options are specified and there aren't too many arguments provided. If you need to capture an arbitrary number of additional arguments, just use *args and **kwargs and you can check that the len is correct yourself.

For nargout, there is no python equivalent because python requires all outputs be handled. So if you do return a, b, in myfun, a, and b must both be handled. You can simply ignore some, but you must explicitly do this. There is no easy way to check if those arguments are being ignored. So say we have this in myfunc:

return a, b, c, d

And you only want a, you can just do:

a, *_ = myfunc()

This puts the first returned value in a and dumps the remainder in the throw-away variable _ (just like *args captures all remaining arguments, *_ captures all remaining outputs). For, say, a and c, you can do:

a, _, c, _ = myfunc()

You can also index directly from a function call, something MATLAB doesn't allow. So this is also possible. So to get the first argument, you can do:

a = myfunc()[0]

So since it is easy in Python to get the returned values you want, and since implicit variable numbers of output values is an extreme pain to handle properly, Python doesn't support it. If you really want to change the number of output argument you can set an optional named argument in the function definition to do so. Since optional arguments are easy in Python, this isn't a huge issue. But it isn't usually needed.

As for errors, yes you just raise the appropriate exception. Python has different types of errors for different situations. raise TypeError('blah') is the correct approach for the wrong number of arguments, where blah is the message you want.

like image 162
TheBlackCat Avatar answered Sep 09 '25 15:09

TheBlackCat