mpappu mpappu - 7 months ago 13
Python Question

Is there a way to interate through all the functions of a module?

I have a list of numbers and I want to, for (for example) the first term and the second term, perform every function in my imported

math
module.

import math
list = [1, 2, 3]
#do stuff that will print (for example) 2+1, 2-1, 2/1, etc.

Answer

Here's a simple method. You'll need to specify what happens if the function doesn't expect two arguments.

for name in dir(math):
    item = getattr(math, name)
    if callable(item):
        item(list[0], list[1])