fork download
  1. import types
  2.  
  3. class ClassOrInstanceMethod(object):
  4. def __init__(self, wrapped):
  5. self.wrapped = wrapped
  6. def __get__(self, instance, owner):
  7. if instance is None:
  8. instance = owner
  9. return self.wrapped.__get__(instance, owner)
  10.  
  11. class demo(object):
  12. @ClassOrInstanceMethod
  13. def foo(self):
  14. # self will be the class if this is called on the class
  15. print(self)
  16.  
  17. demo.foo()
  18. demo().foo()
Success #stdin #stdout 0s 23336KB
stdin
Standard input is empty
stdout
<class '__main__.demo'>
<__main__.demo object at 0x2ae9db54cf90>