Beginning Question about Python functions, parameters...
astral orange
457r0.jp at gmail.com
Mon Nov 23 14:14:35 EST 2009
On Nov 23, 1:17 pm, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
> astral orange wrote:
> > 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):
>
> The zip-function takes n iterables, and produces a list with n-tuples out of
> it. Type this into the python-prompt:
>
> >>> zip([1, 2, 3], ["a", "b", "c"])
>
> The other thing here is tuple-unpacking. If you know that something has a
> specific length, you can unpack it into distinct values like this:
>
> >>> a, b = (10, 20)
> >>> print a
> 10
> >>> print b
>
> 20
>
> Now
>
> for label, name in zip(labels, names):
>
> does
>
> - create a list of tuples, each tuple having two elements, the first being
> the label, the second a name
> - loops over this list
> - for each item in the list (remember, it's a 2-tuple!), unpack it into
> label and name
>
> > 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)
>
> Data here is expected to be a dictionary of dictionaries. The first level of
> keys are the labels. The second is the name. It is expected that labels
> always exist, but names might be empty, so instead of writing
>
> return data[label][name]
>
> it uses get(name) on a dict which will return the value for the key, or
> None:
>
>
>
> >>> {"foo" : "bar"}.get("foo")
> bar
> >>> {"foo" : "bar"}.get("baz")
> >>> # no output means None
>
> That being said, I agree with Neo that this introduction seems to be rather
> bad.
>
> Diez
Thanks all for the replies back! I do appreciate it.
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
into this book so it' what I have to work with. By the way, the book
is, "Apress Beginning Python 2nd Edition"....
But back to the example, on line 104 I see there's a call to the
lookup function, passing 3
parameters ('data', which I think is a nested dictionary, label
(first, middle, last) and name).
But I am getting lost on line 111 after the parameters are passed in
to the lookup function. I'm
not exactly sure what it's returning and when it does return, is this
assigned the the variable
'people'? And if so, I'm not sure what 'append(full_name)' does here.
The else statement which assigns
'[full_name]' to 'data[label][name]' is somewhat confusing as I'm
saying to myself where the heck did
'[full_name]' come "into the picture".
If I could understand what's going on with everything in this the rest
of the chapter and the next
should be fairly easy, but I really need to understand this before I
move on. Thanks again for the
replies back. I am looking over your comments right now.
457r0
More information about the Python-list
mailing list