Beginning Question about Python functions, parameters...

astral orange 457r0.jp at gmail.com
Mon Nov 23 18:13:28 EST 2009


On Nov 23, 4:35 pm, Terry Reedy <tjre... at udel.edu> wrote:
> 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

Wow! First, thanks Terry for the detailed explanation. I'll spend
the rest of the night trying to figure all of this out. :) Also,
thanks to everyone
else that responded as well. I do appreciate you all volunteering your
time to help.
I should of came here sooner.

As far as the program. I did add print statements such as print
(MyNames) and got back:

{'middle': {}, 'last': {'Smith': ['John Larry Smith']}, 'first': {}}

'Smith': ['John Larry Smith'] was in the last key so I will spend some
time figuring
out just why that is the case. I'll spend more time working with the
functions and
hopefully figure this out soon.

Thanks again!

Thanks again for the responses. I'll take all your advice.



More information about the Python-list mailing list