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

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

openopt@ukr.net
Guest
Posts: n/a

 02-18-2007
Dmitrey

Jorge Godoy
Guest
Posts: n/a

 02-18-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:

And those do ... ?

--
Jorge Godoy <(E-Mail Removed)>

Andrew McLean
Guest
Posts: n/a

 02-18-2007
Where you would use varargin and nargin in Matlab, you would use the
*args mechanism in Python.

Try calling

def t1(*args):
print args
print len(args)

with different argument lists

Where you would use varargout and nargout in Matlab you would use tuple
unpacking in Python.

Play with this

def t2(n):
return tuple(range(n))

a, b = t2(2)

x = t2(3)

Dan Bishop
Guest
Posts: n/a

 02-18-2007
On Feb 18, 12:58 pm, (E-Mail Removed) wrote:
> 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):
...

Dennis Lee Bieber
Guest
Posts: n/a

 02-18-2007
On 18 Feb 2007 10:58:07 -0800, (E-Mail Removed) declaimed the following
in comp.lang.python:

> Dmitrey

Something more than?

Microsoft Windows XP [Version 5.1.2600]

C:\Documents and Settings\Dennis Lee Bieber>python
ActivePython 2.4.3 Build 12 (ActiveState Software Inc.) based on
Python 2.4.3 (#69, Apr 11 2006, 15:32:42) [MSC v.1310 32 bit (Intel)] on
win32
>>> def args(*arg):

.... print "Length: ", len(arg)
.... for i, j in enumerate(arg):
.... print "Argument %s: %s" % (i, j)
.... retlen = int(len(arg) / 2 + 0.5)
.... return arg[:retlen]
....
>>> print "return: %s" % args(1, "a", 3)

Length: 3
Argument 0: 1
Argument 1: a
Argument 2: 3
return: 1
>>> print "return: %s" % (args(1, "a", 3, 3.1415926),)

Length: 4
Argument 0: 1
Argument 1: a
Argument 2: 3
Argument 3: 3.1415926
return: (1, 'a')
>>> print "return: %s" % (args(1, "a", 3, 3.1415926, "Hello World", "how many"),)

Length: 6
Argument 0: 1
Argument 1: a
Argument 2: 3
Argument 3: 3.1415926
Argument 4: Hello World
Argument 5: how many
return: (1, 'a', 3)
>>>

--
Wulfraed Dennis Lee Bieber KD6MOG
(E-Mail Removed) (E-Mail Removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (E-Mail Removed))
HTTP://www.bestiaria.com/

openopt@ukr.net
Guest
Posts: n/a

 02-19-2007
Ok, thx
But can I somehow determing how many outputs does caller func require?
for example:
MATLAB:
function [objFunVal firstDerive secondDerive] = simpleObjFun(x)
objFunVal = x^3;
if nargout>1
firstDerive = 3*x^2;
end
if nargout>2
secondDerive = 6*x;
end

So if caller wants only
[objFunVal firstDerive] = simpleObjFun(15)
than 2nd derivatives don't must to be calculated with wasting cputime.
Is something like that in Python?

Peter Otten
Guest
Posts: n/a

 02-19-2007
(E-Mail Removed) wrote:

> Ok, thx
> But can I somehow determing how many outputs does caller func require?
> for example:
> MATLAB:
> function [objFunVal firstDerive secondDerive] = simpleObjFun(x)
> objFunVal = x^3;
> if nargout>1
> firstDerive = 3*x^2;
> end
> if nargout>2
> secondDerive = 6*x;
> end
>
> So if caller wants only
> [objFunVal firstDerive] = simpleObjFun(15)
> than 2nd derivatives don't must to be calculated with wasting cputime.
> Is something like that in Python?

For a sequence whose items are to be calulated on demand you would typically
use a generator in Python. You still have to provide the number of items
somehow (see the head() helper function).

from itertools import islice

def derive(f, x0, eps=1e-5):
while 1:
yield f(x0)
def f(x, f=f):
return (f(x+eps) - f(x)) / eps

return tuple(islice(items, n))

if __name__ == "__main__":
def f(x): return x**6

Peter

Robert Kern
Guest
Posts: n/a

 02-19-2007
(E-Mail Removed) wrote:
> Ok, thx
> But can I somehow determing how many outputs does caller func require?
> for example:
> MATLAB:
> function [objFunVal firstDerive secondDerive] = simpleObjFun(x)
> objFunVal = x^3;
> if nargout>1
> firstDerive = 3*x^2;
> end
> if nargout>2
> secondDerive = 6*x;
> end
>
> So if caller wants only
> [objFunVal firstDerive] = simpleObjFun(15)
> than 2nd derivatives don't must to be calculated with wasting cputime.
> Is something like that in Python?

Return an object with each of the results objFunVal, firstDerive, and
secondDerive as attributes (or a dictionary). Use keyword arguments to inform
the function of which ancillary computations it needs to perform.

If at all possible, don't change the number of return values. It's annoying to
deal with such an API.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
an underlying truth."
-- Umberto Eco

juh
Junior Member
Join Date: Nov 2011
Posts: 1

 11-17-2011
For input arguments, consider using keyword arguments,
very useful in python :

def f(a, b=42, **kargs):
print a*b, 'and', kargs
The constrain is that, appart from a and b, keyword must be used when
passing arguments. However, this feature is really nice and I recommend
to use it as much as possible.

Last edited by juh; 11-17-2011 at 02:27 PM..