By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,665 Members | 1,576 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,665 IT Pros & Developers. It's quick & easy.

Working with multiple return values

dshimer
Expert 100+
P: 136
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
Share this Question
Share on Google+
4 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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
Expert 100+
P: 136
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
Expert Mod 2.5K+
P: 2,851
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
Expert 5K+
P: 6,596
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

Post your reply

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