Does Python have equivalent to MATLAB "varargin", "varargout", "nargin", "nargout"?

Dan Bishop danb_83 at yahoo.com
Sun Feb 18 15:10:04 EST 2007


On Feb 18, 12:58 pm, open... at ukr.net wrote:
> Thank you in advance for your response.
> Dmitrey

The Python equivalent to "varargin" is "*args".

def printf(format, *args):
    sys.stdout.write(format % args)

There's no direct equivalent to "varargout".  In Python, functions
only have one return value.  However, you can simulate the effect of
multiple return values by returning a tuple.

"nargin" has no direct equivalent either, but you can define a
function with optional arguments by giving them default values.  For
example, the MATLAB function

function [x0, y0] = myplot(x, y, npts, angle, subdiv)
% MYPLOT  Plot a function.
% MYPLOT(x, y, npts, angle, subdiv)
%     The first two input arguments are
%     required; the other three have default values.
 ...
if nargin < 5, subdiv = 20; end
if nargin < 4, angle = 10; end
if nargin < 3, npts = 25; end
 ...

would be written in Python as:

def myplot(x, y, npts=25, angle=10, subdiv=20):
    ...




More information about the Python-list mailing list