[issue42765] Introduce new data model method __iter_items__
Richard Neumann
report at bugs.python.org
Wed Jan 6 05:58:49 EST 2021
Richard Neumann <mail at richard-neumann.de> added the comment:
Okay, I found the solution. Not using super() works:
from typing import NamedTuple
class Spamm(NamedTuple):
foo: int
bar: str
def __getitem__(self, index_or_key):
if isinstance(index_or_key, str):
try:
return getattr(self, index_or_key)
except AttributeError:
raise KeyError(index_or_key) from None
return tuple.__getitem__(self, index_or_key)
def keys(self):
yield 'foo'
yield 'bar'
def main():
spamm = Spamm(12, 'hello')
print(spamm.__getitem__)
print(spamm.__getitem__(1))
d = dict(spamm)
print(d)
if __name__ == '__main__':
main()
Result:
<bound method Spamm.__getitem__ of Spamm(foo=12, bar='hello')>
hello
{'foo': 12, 'bar': 'hello'}
----------
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42765>
_______________________________________
More information about the Python-bugs-list
mailing list