Skip to content
Advertisement

How to call __set_item__ at dictionary initialisation?

Below is a class MyDict that inherits from dict. What I want to do is basically to perform some operation each time a key of a MyDict object is assigned a value. So __setitem__ is my friend here:

class MyDict(dict):
    def __setitem__(self, key, value):
        print(f'key {key} set to {value}')
        super().__setitem__(key, value)

m = MyDict()
m[0] = 'hello'   # prints "0 set to hello"
m[1] = 'world!'  # prints "1 set to world!"

This is great but now if I do this, __setitem__ is not called anymore.

m = MyDict({0: 'hello', 1: 'world!'})

I could of course overload __init__ to solve my problem but is there a simpler solution?

Besides, as I understand the dict.__init__ help message:

 |  dict(iterable) -> new dictionary initialized as if via:
 |      d = {}
 |      for k, v in iterable:
 |          d[k] = v

__setitem__ should be called in my example. Have I misunderstood something?

Advertisement

Answer

You should subclass UserDict:

from collections import UserDict

class MyDict(UserDict):
    def __setitem__(self, key, value):
        print(f'key {key} set to {value}')

m = MyDict({0: 'hello', 1: 'world!'})

Outputs

key 0 set to hello
key 1 set to world!

Related questions: Subclass dict: UserDict, dict or ABC? and list vs UserList and dict vs UserDict

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement