473,496 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Working with multiple return values

dshimer
136 Recognized Expert New Member
In the simplest terms...
I have a function that returns 3 values
Expand|Select|Wrap|Line Numbers
  1. >>> def sendnum():
  2. ...     return 1,2,3
and a function that takes 3 arguments.
Expand|Select|Wrap|Line Numbers
  1. >>> def receive(a=4,b=5,c=6):
  2. ...     print a,b,c
They of course do their individual jobs
Expand|Select|Wrap|Line Numbers
  1. >>> sendnum()
  2. (1, 2, 3)
  3. >>> receive(11,12,13)
  4. 11 12 13
Is there any way to code this so that the sending function doesn't send it as a list, or that the receiving function sees it as 3 different values? So instead of
Expand|Select|Wrap|Line Numbers
  1. >>> receive(sendnum())
  2. (1, 2, 3) 5 6
I would get
Expand|Select|Wrap|Line Numbers
  1.  >>> receive(sendnum())
  2. 1 2 3
Jun 21 '07 #1
4 2194
bvdet
2,851 Recognized Expert Moderator Specialist
In the simplest terms...
I have a function that returns 3 values
Expand|Select|Wrap|Line Numbers
  1. >>> def sendnum():
  2. ...     return 1,2,3
and a function that takes 3 arguments.
Expand|Select|Wrap|Line Numbers
  1. >>> def receive(a=4,b=5,c=6):
  2. ...     print a,b,c
They of course do their individual jobs
Expand|Select|Wrap|Line Numbers
  1. >>> sendnum()
  2. (1, 2, 3)
  3. >>> receive(11,12,13)
  4. 11 12 13
Is there any way to code this so that the sending function doesn't send it as a list, or that the receiving function sees it as 3 different values? So instead of
Expand|Select|Wrap|Line Numbers
  1. >>> receive(sendnum())
  2. (1, 2, 3) 5 6
I would get
Expand|Select|Wrap|Line Numbers
  1.  >>> receive(sendnum())
  2. 1 2 3
Expand|Select|Wrap|Line Numbers
  1. >>> receive(*sendnum())
  2. 1 2 3
  3. >>> 
Jun 21 '07 #2
dshimer
136 Recognized Expert New Member
Fantastic, what is terminology for this, so I can study up on it? I have tried it in an system which embeds python to access CAD functions, so I have no control over the supplied objects. This works perfect.

Thanks

Expand|Select|Wrap|Line Numbers
  1. >>> receive(*sendnum())
  2. 1 2 3
  3. >>> 
Jun 21 '07 #3
bvdet
2,851 Recognized Expert Moderator Specialist
Fantastic, what is terminology for this, so I can study up on it? I have tried it in an system which embeds python to access CAD functions, so I have no control over the supplied objects. This works perfect.

Thanks
A function can accept a variable number of arguments if an asterisk (*) precedes the last argument in an argument list:
Expand|Select|Wrap|Line Numbers
  1. >>> def sample(s, *args):
  2. ...     print s
  3. ...     for item in args:
  4. ...         print item
  5. ...         
  6. >>> s = 'A string'
  7. >>> sample(s, 'This', 'is', 'a', 'test')
  8. A string
  9. This
  10. is
  11. a
  12. test
  13. >>> aList = ('This', 'is', 'a', 'test')
  14. >>> sample(s, aList)
  15. A string
  16. ('This', 'is', 'a', 'test')
  17. >>> sample(s, *aList)
  18. A string
  19. This
  20. is
  21. a
  22. test
  23. >>> 
A function can also accept a variable number of keyword arguments if '**' precedes the last argument in an argument list:
Expand|Select|Wrap|Line Numbers
  1. >>> def samplekw(**kargs):
  2. ...     for key in kargs:
  3. ...         print key, kargs[key]
  4. ...         
  5. >>> samplekw(**{'key1': 1, 'key2': 100})
  6. key2 100
  7. key1 1
  8. >>> samplekw(key1=1, key2=100)
  9. key2 100
  10. key1 1
  11. >>> 
Combined:
Expand|Select|Wrap|Line Numbers
  1. >>> def sample(s, *args, **kargs):
  2. ...     print s
  3. ...     for item in args:
  4. ...         print item
  5. ...     for key in kargs:
  6. ...         print key, kargs[key]
  7. ...         
  8. >>> sample(s, *aList, **{'key1': 1, 'key2': 100})
  9. A string
  10. This
  11. is
  12. a
  13. test
  14. key2 100
  15. key1 1
  16. >>> 
Jun 21 '07 #4
bartonc
6,596 Recognized Expert Expert
Fantastic, what is terminology for this, so I can study up on it? I have tried it in an system which embeds python to access CAD functions, so I have no control over the supplied objects. This works perfect.

Thanks
Mark Lutz calls it "varargs" on p 338 of
7. More argument matching examples. Here is the sort of interaction you should get, along with comments that explain the matching that goes on:
Expand|Select|Wrap|Line Numbers
  1. def f1(a, b): print a, b             # normal args
  2.  
  3. def f2(a, *b): print a, b            # positional varargs
  4.  
  5.  f3(a, **b): print a, b           # keyword varargs
  6.  
  7.  f4(a, *b, **c): print a, b, c    # mixed modes
  8.  
  9.  f5(a, b=2, c=3): print a, b, c   # defaults
  10.  
  11.  f6(a, b=2, *c): print a, b, c    # defaults + positional varargs
  12. % python
  13. >>> f1(1, 2)                  # matched by position (order matters)
  14. 1 2
  15. >>> f1(b=2, a=1)              # matched by name (order doesn't matter)
  16. 1 2
  17. >>> f2(1, 2, 3)               # extra positionals collected in a tuple
  18. 1 (2, 3)
  19. >>> f3(1, x=2, y=3)           # extra keywords collected in a dictionary
  20. 1 {'x': 2, 'y': 3}
  21. >>> f4(1, 2, 3, x=2, y=3)     # extra of both kinds
  22. 1 (2, 3) {'x': 2, 'y': 3}
Jun 22 '07 #5

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

Similar topics

17
2405
by: Andrae Muys | last post by:
Found myself needing serialised access to a shared generator from multiple threads. Came up with the following def serialise(gen): lock = threading.Lock() while 1: lock.acquire() try: next...
66
4896
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()
1
2533
by: John Smith | last post by:
I have a user assigned multiple roles and a role can be inherited from multiple parents (see below). How do I answer such questions as "How many roles does the user belongs to?" I answered the...
17
43868
by: Roland Hall | last post by:
Is there a way to return multiple values from a function without using an array? Would a dictionary object work better? -- Roland Hall /* This information is distributed in the hope that it...
4
10630
by: randy.p.ho | last post by:
Using JDBC, is there a way to call a stored procedure with multiple return values? Thanks.
16
17370
by: Nikolay Petrov | last post by:
How can I return multiple values from a custom function? TIA
8
5167
by: aleksandar.ristovski | last post by:
Hello all, I have been thinking about a possible extension to C/C++ syntax. The current syntax allows declaring a function that returns a value: int foo(); however, if I were to return...
2
3650
ADezii
by: ADezii | last post by:
The incentive for this Tip was an Article by the amazing Allen Browne - I considered it noteworthy enough to post as The Tip of the Week in this Access Forum. Original Article by Allen Browne ...
2
2163
by: satyanarayan sahoo | last post by:
Can we write a method which returns multiple values?
5
2105
by: mukeshrasm | last post by:
Hi I am using AJAX to display the value in selection/list box. this code is working fine in Firefox Mozila browser but it is not working in Internet Explorer so please tell me how this will work...
0
7160
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
7196
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...
1
6878
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
7373
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...
1
4897
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...
0
4583
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...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1405
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 ...
1
649
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.