fork download
  1. from inspect import ismethod
  2.  
  3. class Call:
  4. def __call__(self):
  5. pass
  6.  
  7. class Foo:
  8. def __init__(self):
  9. self.var = Call()
  10.  
  11. def method(self):
  12. pass
  13.  
  14. foo = Foo()
  15.  
  16.  
  17. def method_exists(instance, method):
  18. return hasattr(instance, method) and ismethod(getattr(instance, method))
  19.  
  20.  
  21. print(method_exists(foo, "method"))
  22. print(method_exists(foo, "var"))
Success #stdin #stdout 0.02s 30664KB
stdin
Standard input is empty
stdout
True
False