Message314502
| 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 |
|