Borealis
Borealis

Reputation: 8480

How to automatically execute all functions in script?

What is the best practice for automatically executing all of the functions in the script? Take for example this script:

def a():
    return 1

def b():
    return 100

def c():
    return 1000

How would I execute all of these functions without performing the following after running the script:

>>>a()
1
>>>b()
100
>>>c()
1000

Upvotes: 0

Views: 85

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1124000

You can find all function objects in your globals:

from inspect import isfunction

for obj in globals().values():
    if isfunction(obj) and obj.__module__ == __name__:
        print obj()

By testing for the __module__ attribute you filter out any imported function objects (such as the inspect.isfunction() function). This assumes that none of your functions take arguments.

Upvotes: 4

Related Questions