Keyerror problem

Gerhard Häring gerhard.haering at gmx.de
Mon Sep 2 03:18:18 CEST 2002


davbucko wrote in comp.lang.python:
> I have a class Flags as follows:
> 
> class Flags:
>     def __init__(self):
>         self.flags = {}
> 
>     def getInverseFlags(self):
>         for i in self.flags.keys():
>             #switch some keys/values around
> 
>     def setFlag(self,name,value):
>         self.flags[`name`] = value
                     ^^^^^^

Here's the bug. It should read: self.flags[name] = value.

`name` calls repr on name, which is most likely not what you want.

Btw. if you want to invert a dictionary, there are probably hundreds
of examples out there, here's a hopefully readable one:

def invertdict(orig):
    new = {}
    for k, v in orig.items():
        new[v] = k
    return new

Gerhard
-- 
mail:   gerhard <at> bigfoot <dot> de       registered Linux user #64239
web:    http://www.cs.fhm.edu/~ifw00065/    OpenPGP public key id AD24C930
public key fingerprint: 3FCC 8700 3012 0A9E B0C9  3667 814B 9CAA AD24 C930
reduce(lambda x,y:x+y,map(lambda x:chr(ord(x)^42),tuple('zS^BED\nX_FOY\x0b')))



More information about the Python-list mailing list