Beginning Question about Python functions, parameters...

Terry Reedy tjreedy at udel.edu
Mon Nov 23 16:35:43 EST 2009


astral orange wrote:

> Yes, lines 104-111 is really where my problem lies in understanding
> what is going on here.
> I am beginner so this stuff seems a little unwieldy at the moment. :)
> I *did* invest $40

Water under the bridge. Focus on the future...

> into this book so it' what I have to work with. By the way, the book
> is, "Apress Beginning Python 2nd Edition"....

The online tutorial is free. Read it along with the book. Two 
explanations of a particular point are often better than one, at least 
for me.

Now, to where you seem to be stuck. Data is a dict of 3 dicts, one for 
each part of a full [Western] name. Each subdict maps pieces of 
fullnames to a list of fullnames that contain that piece in the 
appropriate position.

This is something like a database of names with 3 fields and an index 
for each field pointing to records in the database, except that there is 
no database. This is why the structure strikes people as a bit strange. 
The records are still there, off in anonymous dataspace, but there is no 
way to access them except via the indexes. If init started with
     data['names'] = [] # empty list, not dict
and the third line of store were
     data['names'].append(names)
then there would be. You might try that as an exercise once you 
understand what is already there.

Anyway, you should "print MyNames" (2.x) or "print(MyNames)" (3.x) after 
storing the first name so you can see the structure of MyNames. The 
tutorial, of course, tells you this. DO read it.

Then add more data and print again to see what changes. For instance,

store(MyNames, 'Susan Smith')
print(MyNames)

Then try several lookups.

store() checks each of the pieces of a name to see whether or not it is 
already in the corresponding key dict. This is the "people =" line. The 
important point in lookup is that when dict d does not contain key k, 
d.get(k) returns None while the usual d[k]raises a KeyError. So lookup() 
always return something, even if just None. So 'people' is either None 
or an exiting list of names. In the latter case, the current full names 
is added to the list. In the former case, the name-piece is associated 
with a new list with one item.

If this is still not clear, add print statements or print function 
statements at appropriate places inside the code for the store function. 
This is how many people clarify thier understanding of a function, 
including when debugging.

Good luck.

Terry Jan Reedy




More information about the Python-list mailing list