/python/super.py

https://github.com/von/sandbox · Python · 27 lines · 14 code · 3 blank · 10 comment · 1 complexity · 440eed118e4bb70c2c5088fdecd78f54 MD5 · raw file

  1. #!/usr/bin/env python3
  2. #
  3. # Kudos: http://stackoverflow.com/questions/576169/python-super
  4. #
  5. class A(object):
  6. def routine(self):
  7. print("A.routine()")
  8. class B(A):
  9. def routine(self):
  10. print("B.routine()")
  11. # Note that the following error:
  12. # "super() argument 1 must be type, not classobj"
  13. # Means the super class is a classic class (i.e. not rooted at
  14. # object) and super won't work. You have to use the class name
  15. # explicitly.
  16. # Kudos: http://www.velocityreviews.com/forums/t338909-help-with-super.html
  17. super(B, self).routine()
  18. if __name__ == '__main__':
  19. a = A()
  20. b = B()
  21. print("Calling a.routine:")
  22. a.routine()
  23. print("Calling b.routine:")
  24. b.routine()