Search code examples
pythonfunctionpython-2.7try-catch

Python check if function exists without running it


In python how do you check if a function exists without actually running the function (i.e. using try)? I would be testing if it exists in a module.


Solution

  • You can use dir to check if a name is in a module:

    >>> import os
    >>> "walk" in dir(os)
    True
    >>>
    

    In the sample code above, we test for the os.walk function.