Keyerror problem

Gerhard Häring gerhard.haering at
Sun Sep 1 21:18:18 EDT 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

mail:   gerhard <at> bigfoot <dot> de       registered Linux user #64239
web:    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