0

Scenario:

I am working with python to execute some functions in cycle... over and over again.

How I do it:

I create a list with all the functions I want to execute, and then I do so.

# Define the functions before using 'em
def this_f:
    pass

def the_other:
    pass

....

# List of f() I want to run
funciones_escrapeadoras = [
    this_f,
    the_other,
    ...
    ]

# Call them all, cyclically
while True:
    for funcion_escrapeadora in funciones_escrapeadoras:
        funcion_escrapeadora()

Question:

If I prefixed all the functions I want to be part of the list, is there a way to automatically identify them and put them into that list?

Example:

I define the functions:

autorun_lalaa, hello_world, autorun_lololo, ...

And only autorun_lalaa and autorun_lololo and autorun_* will be part of the list.

Purpose:

To add in the functions I want to run, without needing to update the list.

4
  • do you have a list of functions prefixed with autorun ? Commented Jul 17, 2017 at 11:43
  • No, that is the list I want python itself to generate, by somehow (<-- here lies my question's core) making python to iterate over all the functions defined in the current .py file and filtering those starting with autorun (or whatever other prefix) Commented Jul 17, 2017 at 11:45
  • 1
    i would suggest make a global list which will be updated by every function with the name of itself Commented Jul 17, 2017 at 11:46
  • @ArpitSolanki great idea, but I will stick to John's answer. Thank you. Commented Jul 17, 2017 at 11:56

2 Answers 2

2

Use the builtin locals() or globals():

for name, obj in locals().iteritems():
    if name.startswith('autorun_'):
        obj()

You could also make a Decorator which is described here: https://wiki.python.org/moin/PythonDecorators - then the functions don't need a name prefix, you could instead have the decorator add the function to a list.

Sign up to request clarification or add additional context in comments.

3 Comments

Perfect, sir. Thank you very much.
Actually the decorator solution is by far the best one - it's highly explicit and as safe and robust as it can be.
Please, consider this, too: stackoverflow.com/questions/30418481/…
1

To list the names of all the functions and variables of a module you can use:

funcs_n_vars = dir(modulename)

Then you can iterate over it to filter the list:

filtered_funcs = [func for func in funcs_n_vars if ('filter' in func)]

Finally, to call the methods from the list, you can do:

for func in filtered_funcs
    method_to_call = getattr(modulename, func)
    result = method_to_call()

1 Comment

Thank you too, but I will go with John's answer. I don't need the functions from a module but from current file, and locals() is the approach I was looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.