473,385 Members | 1,766 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,385 software developers and data experts.

How to determine number of output arguments in Python? (nargout in MATLAB)

Hi guys!

I'm a total newbie in Python. I hope someone could be so kind and help me out here. I'm trying to transfer some MATLAB source codes to Python and run into a problem.

In MATLAB there is a command "nargout" which returns the number of output arguments specified in the call to the currently executing function.
I would like to use similar method for Python in order to decrease the processing time.

The user should be able to choose which output arguments of function he needs and the unchosen one should be ignored and should not be calculated at all. In order to be able to do that, the number of output arguments which user has chosen, need to be determined first. And I absolutely have no idea how to do this.

Example of my code
if the user calls the function like this, all parameters should be calculated and shown.
Expand|Select|Wrap|Line Numbers
  1.  q,r,s = ft(a,b)
  2.  

But if user calls only "q, r". Then "s" should be ignored and not calculated.

Expand|Select|Wrap|Line Numbers
  1. q,r = ft(a,b)
  2.  
Could somebody please kindly advise me on how this works?

Thank you very much in advance for every answer!
Regards,
Anna
Mar 20 '12 #1
3 6952
bvdet
2,851 Expert Mod 2GB
I don't know of a way to do that in Python. You could create a class with methods to make the various calculations. A method would be called only when you need the calculation it performs. The calls would look something like this:
Expand|Select|Wrap|Line Numbers
  1. obj = Ft(a,b) # create an instance, calculate 'q' and 'r' as attributes
  2. q = obj.q
  3. r = obj.r
  4. s = obj.calcs()
Mar 20 '12 #2
dwblas
626 Expert 512MB
Python generally uses a dictionary to keep track of variables. You can send one dictionary to the function with the variables, and the calculation also if necessary, and return a second dictionary of key="q", "r", and/or "s" pointing to the result of the calculation for that variable.
Expand|Select|Wrap|Line Numbers
  1. def ft(a,b, a_dict):
  2.     for var in ["q", "r", "s"]:
  3.         if var in a_dict:
  4.  
  5. a_dict={"q":obj.q, "s":obj.calcs}
  6. return_dict=ft(a,b, a_dict) 
Mar 20 '12 #3
Thank you so much for both answers!! :) I will try both ways! :D
Mar 21 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Ali | last post by:
How to use python in matlab?
66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
4
by: rbt | last post by:
How do I set up a function so that it can take an arbitrary number of arguments? For example, I have a bunch of expenses which may grow or shrink depending on the client's circumstance and a...
2
by: Steven D'Aprano | last post by:
I'm trying to keep an open mind, but I am perplexed about something in Python that strikes me as a poor design. py> def func(a,b): py> print a,b py> func(1) Traceback (most recent call...
10
by: The Directive | last post by:
I read the C FAQ question on passing a variable number of arguments, but it didn't help. The example assumes all arguments are of the same type. I want to create a function "trace" that can be...
3
by: PengYu.UT | last post by:
I quickly searched the group. Like the post said, it is not possible to specify indefinite number of arguments. ...
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: 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...
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: James Mills | last post by:
On Fri, Oct 31, 2008 at 8:49 AM, mark floyd <emfloyd2@gmail.comwrote: Mark, this is correct behavior. You have 3 positional arguments in the function definition. You _must_ aupply _all_ 3 of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.