About `defaultdict.__missing__`

My 2p personally - if a user wants to call setdefault, they should do so explicitly. And if they do that, then beyond nicer syntax and ergonomics, they don’t really need a defaultdict at all.

Does anyone have any actual use that is broken by this change?

I have actually tried to write code overriding defaultdict.__missing__, and calling the original with super. I had to switch out .default_factory for my purpose (selecting a particular factory depending on the key) and rapidly gave up. Simply overriding __getitem__ on a regular dict instead was an order of magnitude less hassle.

That does probably mean very few people have ever done it. But I would say it also means the code written by those who have done it, is highly likely to be easily broken.

class MultiFactoryDefaultDict(defaultdict):
        factories = {'a': list
                    ,'b': int
                    }
        

        def __missing__(self, key):
            tmp = self.default_factory
            self.default_factory = self.factories.get(key, tmp)
            retval = super().__missing__(key)
            self.default_factory = tmp
            return retval