How to check whether a method exists in Python?

前端 未结 9 1198
栀梦
栀梦 2021-01-30 15:53

In the function __getattr__(), if a referred variable is not found then it gives an error. How can I check to see if a variable or method exists as part of an objec

9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-30 16:04

    If your method is outside of a class and you don't want to run it and raise an exception if it doesn't exist:

    'mymethod' in globals()

提交回复
热议问题