johnmahugu

python - A function which returns several values

Jun 14th, 2015
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.69 KB | None | 0 0
  1. """
  2. A function which returns several values
  3.  
  4. When you're not accustomed with Python, it's easy to forget that a function can return just any type of object, including tuples.
  5. This a great to create functions which return several values. This is typically the kind of thing that cannot be done in other languages without some code overhead.
  6. """
  7. >>> def myfunction(a):
  8.  return (a+1,a*2,a*a)
  9. >>> print myfunction(3)
  10. (4, 6, 9)
  11.  
  12. """
  13. You can also use mutiple assignment:
  14. """
  15. >>> (a,b,c) = myfunction(3)
  16. >>> print b
  17. 6
  18. >>> print c
  19. 9
  20. """
  21. And of course your functions can return any combination/composition of objects (strings, integer, lists, tuples, dictionnaries, list of tuples, etc.).
  22. """
Advertisement
Add Comment
Please, Sign In to add comment