[New-bugs-announce] [issue32332] Implement slots support for magic methods added in PEP 560

Yury Selivanov report at bugs.python.org
Fri Dec 15 01:47:42 EST 2017

New submission from Yury Selivanov <yselivanov at gmail.com>:

I propose to add type slots for magic methods introduced by PEP 560.  In the brief discussion on the mailing list Guido OK'd the idea: https://mail.python.org/pipermail/python-dev/2017-December/151262.html

I'll submit a PR that implements this proposal by adding new 'tp_as_class' slot, that points to a PyClassMethods struct, that has two fields: `cm_getitem` and `cm_mro_entries`.

Interestingly, when __class_getitem__ is implemented through the type/slots machinery it becomes a bit faster.  Given the following microbenchmark:

    class Foo:
        def __class_getitem__(cls, o):
            return o

    for _ in range(3):
        t = time.monotonic()
        for i in range(10 ** 8):
            assert i == Foo[i]
        print(f'{time.monotonic() - t:.3f}s')

I see these numbers for the master branch:


and these for the C-slots branch:


components: Interpreter Core
messages: 308365
nosy: gvanrossum, levkivskyi, pitrou, yselivanov
priority: normal
severity: normal
status: open
title: Implement slots support for magic methods added in PEP 560
versions: Python 3.7

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list