Beginning Question about Python functions, parameters...

j jimmy.casey.3 at gmail.com
Mon Nov 23 19:26:42 CET 2009


On Nov 23, 12:37 pm, Neo <n... at picture-art.eu> wrote:
> astral orange schrieb:
>
>
>
> > Hi, I am trying to teach myself Python and have a good book to help me
> > but I am stuck on something and I would like for someone to explain
> > the following piece of code for me and what it's actually doing.
> > Certain parts are very clear but once it enters the "def store(data,
> > full_name): ...." function and the "def lookup()..." function things
> > get a little confusing for me. Specifically, lines 103-108 *and* Lines
> > 110-111.
>
> > Lastly, I am not sure how to print the results I've put into this
> > program either, the book I'm reading doesn't tell me. As you can tell,
> > I am a beginner and I don't truly understand everything that is going
> > on here...a lot, but not all....
>
> > Here is the code:
>
> >  92 def init(data):
> >  93     data['first'] = {}
> >  94     data['middle'] = {}
> >  95     data['last'] = {}
> >  96
> >  97 def store(data, full_name):
> >  98     names = full_name.split()
> > 100     if len(names) == 2: names.insert(1, '')
> > 101     labels = 'first', 'middle', 'last'
> > 103     for label, name in zip(labels, names):
> > 104         people = lookup(data, label, name)
> > 105     if people:
> > 106         people.append(full_name)
> > 107     else:
> > 108         data[label][name] = [full_name]
> > 109
> > 110 def lookup(data, label, name):
> > 111     return data[label].get(name)
> > 112
> > 113
> > 114 MyNames = {}
> > 115 init(MyNames)
> > 116 store(MyNames, 'John Larry Smith')
> > 117 lookup(MyNames, 'middle', 'Smith')
>
> If it tells you so I'm not really sure its a good book - partially for
> teaching you into the unpythonic way to do things (above stuff, if its
> not a counter example, should really go into a class)
>
> Have you tried the tutorial first? Its online and very easy to follow
> from the very beginning but you can also skip parts if you are sure you
> already understand it:
>
> http://docs.python.org/tutorial/
>
> HTH
> Tino

The book is "Apress Beginning Python 2nd Edition". I think what you
are saying is if I don't understand all of the code I should go into a
class? Unfortunately I don't have the money or time to enroll into a
class and even if I did they wouldn't be teaching Python it would be
Java instead...and I've already taken Java before...so that's not
really an option...

Regardless, I understand up to the part "for label, name in zip
(labels, names):" which zips the two sequences 'names' and 'labels'
into a list of tuples...

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:

if people:
    people.append(full_name)
else:
    data[label][name] = [full_name]

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....)

Thanks for the reply back



More information about the Python-list mailing list