f



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

Thank you in advance for your response.
Dmitrey

0
openopt1 (100)
2/18/2007 6:58:07 PM
comp.lang.python 77058 articles. 5 followers. Post Follow

7 Replies
852 Views

Similar Articles

[PageSpeed] 47

openopt@ukr.net writes:

> Thank you in advance for your response.

And those do ... ? 

-- 
Jorge Godoy      <jgodoy@gmail.com>
0
jgodoy (109)
2/18/2007 7:17:33 PM
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)

0
2/18/2007 7:48:09 PM
On Feb 18, 12:58 pm, open...@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):
    ...

0
danb_83 (421)
2/18/2007 8:10:04 PM
On 18 Feb 2007 10:58:07 -0800, openopt@ukr.net declaimed the following
in comp.lang.python:

> 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.
>>> 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
	wlfraed@ix.netcom.com		wulfraed@bestiaria.com
		HTTP://wlfraed.home.netcom.com/
	(Bestiaria Support Staff:		web-asst@bestiaria.com)
		HTTP://www.bestiaria.com/
0
wlfraed (4596)
2/18/2007 8:26:14 PM
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?

0
openopt1 (100)
2/19/2007 7:56:15 AM
openopt@ukr.net 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

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

0
__peter__ (4031)
2/19/2007 9:25:22 AM
openopt@ukr.net 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
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

0
robert.kern (1692)
2/19/2007 4:08:57 PM
Reply: