[Tutor] Returning multiple objects from a function
Alexander Q.
redacted@example.com
Tue Jul 3 04:23:15 CEST 2012
I understand the basics of tuples, but that formulation returned the
following error:
Traceback (most recent call last):
File "C:\Users\Owner\Desktop\MIT\Sets\Set3.py", line 34, in <module>
list4 = tuplesresult[1]
TypeError: 'NoneType' object is not subscriptable
When I tried to assign "tuplesresult[1]" to the variable "list4" (after
assigning tuplesresult = mainFunc(), which is the name of the function that
returns the tuple in my program), the error occurred. That aside, is it all
right if I just code "return list1, list2" without the parens? In that
case, how would I access list1 and list2 when needed?
Thanks for your help.
On Mon, Jul 2, 2012 at 4:11 PM, Walter Prins <wprins at gmail.com> wrote:
> On 2 July 2012 23:55, Alexander Q. <redacted@example.com> wrote:
> > Hello- I'm wondering how to access specific objects returned from a
> function
> > when that function returns multiple objects.
> >
> > For example, if I have "return(list1, list2, list 3)" within a function
>
> When you have:
> return (list1, list2, list3)
>
> ... you're actually returning a single tuple object. Read about
> tuples in the documentation. To access an item from a tuple you index
> into it, e.g.
>
> tupleresult = myfunc()
> x = tupleresult[0]
>
> ... for example
>
> Walter.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120702/95fe98ab/attachment.html>
More information about the Tutor
mailing list
html>