Richard Neumann <m...@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 <rep...@bugs.python.org>
<https://bugs.python.org/issue42765>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to