473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

Thank you in advance for your response.
Dmitrey

Feb 18 '07 #1
6 12357
op*****@ukr.net writes:
Thank you in advance for your response.
And those do ... ?

--
Jorge Godoy <jg****@gmail.com>
Feb 18 '07 #2
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)

Feb 18 '07 #3
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):
...

Feb 18 '07 #4
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?

Feb 19 '07 #5
op*****@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

Feb 19 '07 #6
op*****@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

Feb 19 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ali | last post by:
How to use python in matlab?
5
by: mbbx6spp | last post by:
Hi All, I already searched this newsgroup and google groups to see if I could find a Python equivalent to Perl's Template::Extract, but didn't find anything leading to a Python module that had...
9
by: Carl | last post by:
I am desperately looking for a way to call Python from Matlab. I have become used to Python's rich syntax and large number of libraries, and feel ridiculously clumsy being stuck with Matlab's...
2
by: mirandacascade | last post by:
Situation is this: 1) must write application that does the following: a) creates an xml document, the contents of which, is a request transaction b) send xml document to destination; I am...
2
by: N/A | last post by:
Hi all, Can I have your opinions on Matlab vs. Matplotlib in Python in terms of 2D and 3D graphical presentation please. Matplotlib in Python vs. Matlab, which one has much better graphical...
2
by: ycoci0 | last post by:
Hi all, Not exactly new to Python, just have not programmed a time dependent function using it before. I imagine that many of you may also program some JavaScript and may be familiar with...
10
by: Chao | last post by:
I've been trying to develop some numerical codes with python, however got disappointed. A very simple test, a = 1.0 for i in range(1000): for j in range(1000): a = a+1
0
by: Gabriel Genellina | last post by:
En Thu, 08 May 2008 09:11:56 -0300, Michael Mabin <d3vvnull@gmail.comescribió: Like this? <code> import sys,fileinput for line in fileinput.input(inplace=True):...
22
by: Kurien Mathew | last post by:
Hello, Any suggestions on a good python equivalent for the following C code: while (loopCondition) { if (condition1) goto next; if (condition2) goto next;
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.