Skip to content

Instantly share code, notes, and snippets.

@tadone
Last active November 7, 2017 12:38
Show Gist options
  • Save tadone/1ebef2fb079520d3c3592af41f3f9fca to your computer and use it in GitHub Desktop.
Save tadone/1ebef2fb079520d3c3592af41f3f9fca to your computer and use it in GitHub Desktop.
[Return Multiple Values from Functions]
# To return multiple values from a function, simply return a tuple. For example:
>>> def myfun():
... return 1, 2, 3
...
>>> a, b, c = myfun()
>>> a
1
>>> b
2
>>> c
3
# More advanced example:
def defineAList():
local_list = ['1','2','3']
return local_list
def useTheList(passed_list):
print "For checking purposes: in useTheList, list is", passed_list
def main():
# returned list is ignored
returned_list = defineAList()
# passed_list inside useTheList is set to what is returned from defineAList
useTheList(returned_list)
main()
# You can even skip the temporary returned_list and pass the returned value directly to useTheList:
def main():
# passed_list inside useTheList is set to what is returned from defineAList
useTheList(defineAList())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment