This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author yselivanov
Recipients gvanrossum, levkivskyi, pitrou, yselivanov
Date 2017-12-15.06:47:42
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1513320462.57.0.213398074469.issue32332@psf.upfronthosting.co.za>
In-reply-to
Content
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:

17.161s
17.024s
16.530s

and these for the C-slots branch:

15.010s
15.693s
15.035s
History
Date User Action Args
2017-12-15 06:47:42yselivanovsetrecipients: + yselivanov, gvanrossum, pitrou, levkivskyi
2017-12-15 06:47:42yselivanovsetmessageid: <1513320462.57.0.213398074469.issue32332@psf.upfronthosting.co.za>
2017-12-15 06:47:42yselivanovlinkissue32332 messages
2017-12-15 06:47:42yselivanovcreate