[Tutor] Returning multiple objects from a function

Alexander Q. redacted@example.com
Wed Jul 4 04:45:53 CEST 2012


On Mon, Jul 2, 2012 at 8:56 PM, Dave Angel <d at davea.name> wrote:

> On 07/02/2012 10:23 PM, Alexander Q. wrote:
> > 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?
>
> That's no different:   list1, list2  is a tuple of size 2.
> > Thanks for your help.
> >
> You top-posted, which loses all the context of the earlier messages.
> Please put your new message AFTER the part you're quoting
>
> Anyway, you never showed the function mainFunc(), but from the error
> traceback message, it didn't return anything, which means None.
>
> Chances are you have something like:
>
> def mainFunc():
>       if  something:
>             return 4, 12
>
>
> And if the if fails, the function will return None.  To fix that, make
> sure all paths through the function return a similar object, generally a
> tuple of the same size.
>
>
>
> --
>
> DaveA
>
> Ok thanks- I will try that.

-Alex

P.S. Let me know if this message is sent incorrectly (I scrolled down to
the bottom of the text box to type this, as opposed to writing the message
at the top of the text box. I am guessing this is what is meant by
"top-posting.").
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120703/765a902b/attachment.html>


More information about the Tutor mailing list
l>