Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
__getattr__ invoked on __iter__ ?
# Is __getattr__ invoked on __iter__ ?
# Copyright (C) 2021 Rodrigo Silva (MestreLion) <linux@rodrigosilva.com>
# License: GPLv3 or later, at your choice. See <http://www.gnu.org/licenses/gpl>
import collections.abc
import timeit
class MyDict(collections.abc.MutableMapping):
def __init__(self, *a, **kw): self._items = dict(*a, **kw)
def __getitem__(self, key): print(f"get({key})"); return self._items[key]
def __len__(self): return len(self._items)
def __setitem__(self, key, value): self._items[key] = value
def __delitem__(self, key): del self._items[key]
def __contains__(self, key): return key in self._items
def __str__(self): return self.__class__.__name__
def __iter__(self): return iter(self._items)
d = MyDict(dict(one=1, two=2, three=3))
for item in d.items():
print(item)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment