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


Ad

Advertisements

A

Andrew McLean

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)
 
D

Dan Bishop

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):
...
 
D

Dennis Lee Bieber

Thank you in advance for your response.
Dmitrey

Something more than?

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

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
Type "help", "copyright", "credits" or "license" for more information..... 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]
....Length: 3
Argument 0: 1
Argument 1: a
Argument 2: 3
return: 1Length: 4
Argument 0: 1
Argument 1: a
Argument 2: 3
Argument 3: 3.1415926
return: (1, 'a')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 address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
O

openopt

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?
 
Ad

Advertisements

P

Peter Otten

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

def head(items, n):
return tuple(islice(items, n))

if __name__ == "__main__":
def f(x): return x**6
print head(derive(f, 1), 4)

Peter
 
R

Robert Kern

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
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
Ad

Advertisements

juh

Joined
Nov 17, 2011
Messages
1
Reaction score
0
use of key arguments

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:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top