473,546 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Thank you in advance for your response.
Dmitrey

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

--
Jorge Godoy <jg****@gmail.c om>
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.writ e(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(it ems, 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
4690
by: Ali | last post by:
How to use python in matlab?
5
2058
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 similar functionality. I am a big fan of Python as an OO language and use it for many system admin utilities, webdev and even MS Excel AddIn...
9
5280
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 rather restricted facilities for doing other things than standard mathematical work. Does anyone know of good techniques (or readily available...
2
2989
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 assuming that a process at destination side processes the request and sends back a response c) the application I'm writing must receive response and then...
2
2675
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 pressentation?
2
2633
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 JavaScript's SetTimeout( expression, after time interval in milliseconds) function. Does Python have an equivalent to this? A SetTimeout-like Python...
10
5251
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
1239
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): sys.stdout.write(line.replace('mike','dave'))
22
7895
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
7435
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7698
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7947
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7461
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7794
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5361
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3492
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1922
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.