I'm trying to figure out what methods are called when a dict is unpacked, so that I can customize the process?
I suppose the following hack would have shown me the methods called during any method access of Dict
instances:
class Dict(dict):
def __getattribute__(self, name):
print(name)
return super().__getattribute__(name)
But the following session shows that no method is called to perform dict unpacking?
In [1]: d = Dict(a=1)
__class__
__class__
In [2]: {**d}
Out[2]: {'a': 1}
So what is actually happening here? How can I customize the unpacking process?
Edit
I don't think the question is a duplicate of the other one. Even implementing all the special methods mentioned in answers of that question, no of them are called during dict unpacking.
In [66]: class Dict(dict):
...: def __getattribute__(self, name):
...: print(name)
...: return super().__getattribute__(name)
...: def keys(self):
...: print("hello")
...: return super().keys()
...: def __getitem__(self, key):
...: print("hello")
...: return super().__getitem__(key)
...: def __len__(self):
...: print("hello")
...: return super().__len__()
...: def __iter__(self):
...: print("hello")
...: return super().__iter__()
...:
In [67]: d = Dict(a=1)
__class__
__class__
In [68]: {**d}
Out[68]: {'a': 1}
You can see no print
line is called whatsoever. So my question remains unanswered.
FWIW, the python version is Python 3.6.5.