Why can't I delete from a self inherited dictionary?

18 Views Asked by At

I had implemented a dictionary using inherited mechanism like this:

class NoCaseDict(LoggingHandler, FrozenBase, OrderedDict): ....

Then when I load data from a file, I use this to store data:

def makeLowerCaseKey(self, m: Message, using_string=False):
    if using_string:
        mid = m.string.lower()
    else:
        mid = m.id.lower()
    return (mid, m)

dict_data_list = [self.makeLowerCaseKey(m, using_string=self.is_using_string) for m in data if len(m.id) > 0]

When I tried to do:

del self[key.lower()]

I got exception, only printing out the [key.lower()] part. I had to resorted to the method mentioning in this link: (https://www.geeksforgeeks.org/python-ways-to-remove-a-key-from-dictionary/)

new_dict = {k: v for (k, v) in self.items() if (k != lower_key)}
self.clear() 
self.update(new_dict)

Though lengthy and worked, I do not know why the command:

del self[key.lower()]

doesn't work. Do you know why? I am using Python 3.9.6 on Monterey, macOS.

0

There are 0 best solutions below