0

I have a module that is loaded and I go through all of it's functions to check if they have a certain parameter. If they do, I run those functions;

The problem is the module will also have it's own imports, which add to the function list and some of the functions of the imported module will be run as well. I need to check if one function, given it's address belongs to a certain module.

import bar;
def function(special_param):
    #dostuff

In bar I have a function named other_function with special_param as well. By importing it, Python imports all of it's functions. How do I check if other_function belongs to bar?

Alexandru Antochi
  • 1,295
  • 3
  • 18
  • 42
  • 2
    Possible duplicate of [Determine if a function is available in a Python module](http://stackoverflow.com/questions/763971/determine-if-a-function-is-available-in-a-python-module) – nir0s Mar 07 '17 at 09:18

2 Answers2

0
if hasattr(bar, 'other_function'):
    do_something()

Note that other_function might also be a variable in bar, not a function.

EDIT: This is a duplicate of: Determine if a function is available in a Python module where a better answer by @macrog is given. Use it instead. (flagged your question as duplicate)

Community
  • 1
  • 1
nir0s
  • 1,151
  • 7
  • 17
0

You can try this,

if hasattr(bar, 'other_function'):
        getattr(bar, 'other_function')('Special_parameter')
Chanda Korat
  • 2,453
  • 2
  • 19
  • 23