Created
October 18, 2021 03:09
-
-
Save MestreLion/25953459062b96a74820d2b3193dfdb6 to your computer and use it in GitHub Desktop.
__getattr__ invoked on __iter__ ?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Is __getattr__ invoked on __iter__ ? | |
# Copyright (C) 2021 Rodrigo Silva (MestreLion) <[email protected]> | |
# 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