Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Python: super(ClassName, self) vs. super(self.__class__, self)

View gist:1935894
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
"""Simple example showing why using super(self.__class__, self) is a BAD IDEA (tm)"""
 
class A(object):
def x(self):
print "A.x"
 
class B(A):
def x(self):
print "B.x"
super(B, self).x()
 
class C(B):
def x(self):
print "C.x"
super(C, self).x()
 
 
class X(object):
def x(self):
print "X.x"
 
class Y(X):
def x(self):
print "Y.x"
super(self.__class__, self).x() # <- this is WRONG don't do it!
 
class Z(Y):
def x(self):
print "Z.x"
super(self.__class__, self).x() # <- this is WRONG don't do it!
 
if __name__ == '__main__':
C().x()
Z().x() # will cause 'RuntimeError: maximum recursion depth exceeded'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.