-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use super(cls, self) instead of super() or hard-coded class? #1061
Comments
I'm fine with any improvement to usability as long as we don't significantly regress performance. As to tests, I don't, but when i Optimized this I simply did some profiling on the test suite and/or some of the examples in the examples suite. Lately I've had the best experiance using yappi together with KCacheGrind. |
Here are some very quick timing results. I test the following inheritance chain (not totally trivial, needs to be traversed to the top): class A(object):
def __init__(self, a):
self.a = a
class B(A):
pass
class C(object):
pass
class D1(C, A):
def __init__(self, a, b):
super().__init__(a) # Invalid in Python 2
self.b = b
class D2(C, A):
def __init__(self, a, b):
super(D2, self).__init__(a) # Works with Python 2 and 3
self.b = b
class D3(C, A):
def __init__(self, a, b):
A.__init__(self, a) # Hard-coded, no MRO involved
self.b = b Running
This seems to summarize quite well the experience from the past, namely that using the backported I'd say this is a viable option (given that this behavior shows up also in practice, but why shouldn't it). |
MAINT: unify usage of calls to parent classes, closes #1061
So far we always use e.g.
super().__init__(...)
in subclasses and get Python 2 compatibility by importing asuper
backport from thefuture
library. That is, however, slow, and thus a bunch of speed-critical classes already replace this by hard-codedParentClass.__init__(self, ...)
calls.This is not very pretty and particularly unfriendly towards downstream subclassers. Thus, an alternative could be to use the built-in
super
in both Python 2 and 3 and stick to the (required in Py2, redundant but valid in Py3) syntaxsuper(cls, self).__init__(...)
.@adler-j do you have any good test lying around to check the speed of this compared to
super
fromfuture
and hard-coded parents?The text was updated successfully, but these errors were encountered: