[Tutor] Returning compound objects?

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Thu Nov 16 23:55:17 CET 2006



On Thu, 16 Nov 2006, Chris Hengge wrote:

> I didn't send this to the list because I didn't want to go off-topic.

[Meta]

Then let's start a new topical thread.  Let's call this one "Returning 
compound objects?" and work from there.

But please let's keep this on Tutor;  I'm serious when I say that I don't 
have much time these days to help out on python-tutor, and I want to make 
sure I don't become a single point of failure.  I put myself on Digest 
mode for a reason.  It's not because I don't want to help, but because I 
have other timely obligations that I can't ignore anymore.

See:

     http://catb.org/esr/faqs/smart-questions.html#noprivate

---------------------------------------------------------------------


> Could you please explain how this works?
>
> return names + subnames
>
> how do you catch something like that? Normally I'd do something like
>
> def foo():
>   #do impressive stuff
>   return impressive_stuff
>
> impressive_stuff = foo()
>
> I was asking someone the other day about returning multiple items and I 
> got some huge answer about needing to pack the items into a single item, 
> then unpack them on the other side.


Try this at the interactive interpreter:

#######################################
words = ['this', 'is', 'a', 'sentence']
w1, w2, w3, w4 = words
#######################################

What do you think will happen?  Can you explain what's going on?


Then take a look at this:

########################################
words = ['this', 'is', 'a', 'sentence']
w1, w2 = words
#########################################

What do you think will happen?  And then try it.  Can you say in words 
what happened?  Try explaining it.


Finally, take a look at this:

#######################################
def quotient_and_remainder(x, y):
     return [x / y, x % y]

results = quotient_and_remainder(17, 5)
print results

q, r = quotient_and_remainder(17, 5)
print q
print r
########################################

Predict what you think will show up, and then see if your mental model 
matches what actually happens.


More information about the Tutor mailing list