Skip to content

Instantly share code, notes, and snippets.

@ulope
Created February 28, 2012 23:06
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ulope/1935894 to your computer and use it in GitHub Desktop.
Save ulope/1935894 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