fork download
  1. class Foo(object):
  2. def __init__(self, text):
  3. self.text = text
  4.  
  5. @classmethod
  6. def bar(cls):
  7. return None
  8.  
  9. class hybridmethod:
  10. def __init__(self, f):
  11. self.f = f
  12.  
  13. def __get__(self, obj, cls=None):
  14. if obj is not None:
  15. return self.f.__get__(obj)
  16. return getattr(super(cls, cls), self.f.__name__).__get__(cls)
  17.  
  18. class MyFoo(Foo):
  19. @hybridmethod
  20. def bar(self):
  21. return self.text
  22.  
  23. print(MyFoo.bar()) # outputs None
  24. foo = MyFoo('foo')
  25. print(foo.bar())
Success #stdin #stdout 0.13s 14152KB
stdin
Standard input is empty
stdout
None
foo