473,326 Members | 2,196 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,326 software developers and data experts.

Function or Method return

440 256MB
Hi ,


I would like to return more than 1 value or objects using Functions or Methods.Does Python supports this ca[pability?.If not how I can return more than 1 value.

in Case of C++ I can return more than one value by Call by reference or Call by address.

Thanks in advance
PSB
Feb 23 '07 #1
4 2432
bartonc
6,596 Expert 4TB
Hi ,


I would like to return more than 1 value or objects using Functions or Methods.Does Python supports this ca[pability?.If not how I can return more than 1 value.

in Case of C++ I can return more than one value by Call by reference or Call by address.

Thanks in advance
PSB
Expand|Select|Wrap|Line Numbers
  1. def ReturnMoreThanOne():
  2.     return 1, 2, 3
  3.  
  4. a, b, c = ReturnMoreThanOne()
  5. print a
  6. print b
  7. print c
Feb 23 '07 #2
Motoma
3,237 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. def ReturnMoreThanOne():
  2.     return 1, 2, 3
  3.  
  4. a, b, c = ReturnMoreThanOne()
  5. print a
  6. print b
  7. print c
Another way is to return a tuple:

Expand|Select|Wrap|Line Numbers
  1. def retTuple():
  2.     return (4,7)
  3.  
  4. fourSeven = retTuple()
  5. (four, seven) = retTuple()
  6.  
Python is so nice, isn't it?
Feb 23 '07 #3
bvdet
2,851 Expert Mod 2GB
Expand|Select|Wrap|Line Numbers
  1. def ReturnMoreThanOne():
  2.     return 1, 2, 3
  3.  
  4. a, b, c = ReturnMoreThanOne()
  5. print a
  6. print b
  7. print c
Right on Barton. However it only appears that multiple items are being returned. The 3 values are actually returned as a tuple. This also works:
Expand|Select|Wrap|Line Numbers
  1. >>> def ReturnMoreThanOne():
  2. ...     return [1, 2, 3]    # returned as a list
  3. ... 
  4. >>> a,b,c = ReturnMoreThanOne()
  5. >>> a
  6. 1
  7. >>> b
  8. 2
  9. >>> c
  10. 3
  11. >>> 
Feb 23 '07 #4
bartonc
6,596 Expert 4TB
Right on Barton. However it only appears that multiple items are being returned. The 3 values are actually returned as a tuple. This also works:
Expand|Select|Wrap|Line Numbers
  1. >>> def ReturnMoreThanOne():
  2. ...     return [1, 2, 3]    # returned as a list
  3. ... 
  4. >>> a,b,c = ReturnMoreThanOne()
  5. >>> a
  6. 1
  7. >>> b
  8. 2
  9. >>> c
  10. 3
  11. >>> 
Yeah, I know. Just keeping it simple for the newbie. I prefer motoma's tuple example (the term you used but gave list syntax). There are some tricks to making sure that unpacking takes place correctly (especially when working with library functions that return a variable sized tuple). I've had to do things like
Expand|Select|Wrap|Line Numbers
  1. a, b = somefunc()[-2:]
.
Feb 23 '07 #5

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

Similar topics

9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
8
by: Lucas Lemmens | last post by:
Dear pythonians, I've been reading/thinking about the famous function call speedup trick where you use a function in the local context to represent a "remoter" function to speed up the 'function...
2
by: Edward Diener | last post by:
In C++ an overridden virtual function in a derived class must have the exact same signature of the function which is overridden in the base class, except for the return type which may return a...
14
by: Mr Newbie | last post by:
I am often in the situation where I want to act on the result of a function, but a simple boolean is not enough. For example, I may have a function called isAuthorised ( User, Action ) as ?????...
26
by: Patient Guy | last post by:
The code below shows the familiar way of restricting a function to be a method of a constructed object: function aConstructor(arg) { if (typeof(arg) == "undefined") return (null);...
20
by: Andrew Morton | last post by:
Is it possible to have two function declarations which take the same parameters but return different types depending on how the function is used? function f(x) as string ' return a string end...
7
by: Terry Olsen | last post by:
How do I get this to work? It always returns False, even though I can see "This is True!" in the debug window. Do I have to invoke functions differently than subs? Private Delegate Function...
8
by: Steven D'Aprano | last post by:
I'm writing a factory function that needs to use keywords in the produced function, not the factory. Here's a toy example: def factory(flag): def foo(obj, arg): if flag: # use the spam keyword...
8
by: Viktor | last post by:
Can somebody give me an explanation what happened here (or point me to some docs)? Code: HMMM = None def w(fn): print 'fn:', id(fn) HMMM = fn
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.