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

Working with multiple return values

dshimer
136 Expert 100+
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 2193
bvdet
2,851 Expert Mod 2GB
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 Expert 100+
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 Expert Mod 2GB
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 Expert 4TB
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
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
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
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
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
by: randy.p.ho | last post by:
Using JDBC, is there a way to call a stored procedure with multiple return values? Thanks.
16
by: Nikolay Petrov | last post by:
How can I return multiple values from a custom function? TIA
8
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
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
by: satyanarayan sahoo | last post by:
Can we write a method which returns multiple values?
5
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
0
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
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...

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.