Skip to content

Instantly share code, notes, and snippets.

@qwdm
Forked from ulope/gist:1935894
Created June 8, 2018 16:47
Show Gist options
  • Save qwdm/2a9d1e80890e6527516011478ce755e1 to your computer and use it in GitHub Desktop.
Save qwdm/2a9d1e80890e6527516011478ce755e1 to your computer and use it in GitHub Desktop.
Python: super(ClassName, self) vs. super(self.__class__, self)
"""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