Serhiy Storchaka <storchaka+cpyt...@gmail.com> added the comment:

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

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue33144>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to