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 serhiy.storchaka
Recipients mark.dickinson, rhettinger, serhiy.storchaka, tim.peters, wolma
Date 2018-03-27.07:04:37
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1522134277.84.0.467229070634.issue33144@psf.upfronthosting.co.za>
In-reply-to
Content
I think this is excellent application of __init_subclass__. It is common to patch an instance method in __init__, but this can create a reference loop if patch it by other instance method. In this case the choice doesn't depend on arguments of __init__, and can be done at class creation time.

I like the idea in general, but have comments about the implementation.

__init_subclass__ should take **kwargs and pass it to super().__init_subclass__(). type(cls.random) is not the same as type(self.random). I would use the condition `cls.random is _random.Random.random` instead, or check if the method is in cls.__dict__.

This will break the case when random or getrandbits methods are patched after class creation or per instance, but I think we have no need to support this.

We could support also the following cases:

1.
    class Rand1(Random):
        def random(self): ...
        # _randbelow should use random()

    class Rand2(Rand1):
        def getrandbits(self): ...
        # _randbelow should use getrandbits()
        # this is broken in the current patch

2.
    class Rand1(Random):
        def getrandbits(self): ...
        # _randbelow should use getrandbits()

    class Rand2(Rand1):
        def random(self): ...
        # _randbelow should use random()
        # this is broken in the current code
History
Date User Action Args
2018-03-27 07:04:37serhiy.storchakasetrecipients: + serhiy.storchaka, tim.peters, rhettinger, mark.dickinson, wolma
2018-03-27 07:04:37serhiy.storchakasetmessageid: <1522134277.84.0.467229070634.issue33144@psf.upfronthosting.co.za>
2018-03-27 07:04:37serhiy.storchakalinkissue33144 messages
2018-03-27 07:04:37serhiy.storchakacreate