Beginning Question about Python functions, parameters...

Peter Otten __peter__ at web.de
Tue Nov 24 08:11:58 EST 2009


Terry Reedy wrote:

> astral orange wrote:
> 
>> As far as the program. I did add print statements such as print
>> (MyNames) and got back:
>> 
>> {'middle': {}, 'last': {'Smith': ['John Larry Smith']}, 'first': {}}
> 
> Hmmm, as I understood the code, either that should be ... 'last': {} ...
> before the first store(), as you seem to be thinking below, or after the
> first store(),
> 
> {'middle': {'Larry': ['John Larry Smith'],
>   'last':   {'Smith': ['John Larry Smith'],
>   'first':  {'John' " ['John Larry Smith']}
> 
> or the same with [['John','Larry','Smith']] for each entry (do not
> remember exactly what was stored. Maybe a typo.

That's a bug in the store() function

# as posted
def store(data, full_name):
    names = full_name.split()
    if len(names) == 2: names.insert(1, '')
    labels = 'first', 'middle', 'last'
    for label, name in zip(labels, names):
        people = lookup(data, label, name)
    if people:
        people.append(full_name)
    else:
        data[label][name] = [full_name]

# what was probably intended
def store(data, full_name):
    names = full_name.split()
    if len(names) == 2: names.insert(1, '')
    labels = 'first', 'middle', 'last'
    for label, name in zip(labels, names):
        people = lookup(data, label, name)
        if people:
            people.append(full_name)
        else:
            data[label][name] = [full_name]


Peter




More information about the Python-list mailing list