Beginning Question about Python functions, parameters...

Stephen Hansen apt.shansen at gmail.com
Mon Nov 23 14:33:37 EST 2009


On Mon, Nov 23, 2009 at 10:26 AM, j <jimmy.casey.3 at gmail.com> wrote:

> What I am not totally sure about is when the store function callsthe
> lookup function and does "return data[label].get(name)", that line
> "trips" me up some....then the lookup function returns that back to
> the store function, assigns the data to the variable 'people', THEN
> does this, which also isn't that clear to me what it's all doing:
>
>
It does it right when it hits it in the code; basically, the for loop runs
over the list of items from the zip.

In each one, it calls the lookup function. Execution then jumps over to
lookup. Once the lookup function is done ('returns' or hits its it),
execution jumps right back with a result, and that result is stored int he
'people' variable.

Then the next item in the for loop is processed; again, when the function()
is called, execution jumps over to that again, and so on.

Only when all the things are done in the for loop, does execution move on
to...


> if people:
>    people.append(full_name)
> else:
>     data[label][name] = [full_name]
>
>
Basically, "lookup" will return one of two things.

If someone with the given part of a name already has been stored, it'll
return a list containing the people with that name.

Otherwise, it will return None.

The if/else above will operate on those two conditions: if 'lookup' returned
a list of people, its going to add this person to that list. If it doesn't
return a list of people, it's going to add this persons name to the data
dictionary as a new list. So anyone in the future who has that name will
find them on lookup.

The thing is, I think lines 105-108 should be indented deeper one level to
be 'inside' the for loop; and thus called with each execution, and not only
after the for is all done with. As it is, it seems like a bug. Or I don't
get at all what the point of the code is :)



> My main problem is finding out what's it's actually *doing*? I know
> and understand the terminologies (what 'append' does, what a
> 'sequence' is, a 'list', a 'tuple', 'if'...'else'...etc...et....)
>

I concur with the other people who say this is bad code to learn from; the
book could be a LOT clearer. I'd try another resource.. the online Python
tutorial is a good place to start. A different book may be in order :)


--S
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091123/4433589c/attachment-0001.html>


More information about the Python-list mailing list