Skip to content

Instantly share code, notes, and snippets.

@MestreLion
Created October 18, 2021 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MestreLion/25953459062b96a74820d2b3193dfdb6 to your computer and use it in GitHub Desktop.
Save MestreLion/25953459062b96a74820d2b3193dfdb6 to your computer and use it in GitHub Desktop.
__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