fork(3) download
  1. class Base(object):
  2. @classmethod
  3. def new(cls):
  4. print("Base.new(%r)" % (cls,))
  5. return cls()
  6.  
  7. class UseSuper(Base):
  8. @classmethod
  9. def new(cls):
  10. print("UseSuper.new(%r)" % (cls,))
  11. return super(UseSuper, cls).new() # passes cls as the first arg
  12.  
  13. class NoSuper(Base):
  14. @classmethod
  15. def new(cls):
  16. print("NoSuper.new(%r)" % (cls,))
  17. return Base.new() # passes Base as the first arg
  18.  
  19. class UseFunc(Base):
  20. @classmethod
  21. def new(cls):
  22. print("UseFunc.new(%r)" % (cls,))
  23. return Base.new.im_func(cls) # or `.__func__(cls)`. # passes cls as the first arg
  24.  
  25. print(UseSuper.new())
  26. print('-'*60)
  27. print(NoSuper.new())
  28. print('-'*60)
  29. print(UseFunc.new())
Success #stdin #stdout 0.02s 6356KB
stdin
Standard input is empty
stdout
UseSuper.new(<class '__main__.UseSuper'>)
Base.new(<class '__main__.UseSuper'>)
<__main__.UseSuper object at 0x9bb75cc>
------------------------------------------------------------
NoSuper.new(<class '__main__.NoSuper'>)
Base.new(<class '__main__.Base'>)
<__main__.Base object at 0x9bb75cc>
------------------------------------------------------------
UseFunc.new(<class '__main__.UseFunc'>)
Base.new(<class '__main__.UseFunc'>)
<__main__.UseFunc object at 0x9bb75cc>