0

I've got a list of strings and I want to generate functions like this:

f_names = ["func1", "func2", "func3"] #and possibly many more   
def generate_functions():
    for funcname in f_names:
        def <funcname value>(*args):
            another_function(<funcname value>, *args)

Obviously I can't do that with code sample above.

What is the best way to accomplish my goal? Is it:

  • Generating temporary .py file with all def instructions and importing everything from it?

  • Using exec() and generating def instructions in string?

  • Using classes and __call__()?

  • Using Python built-in tools? (If so what tools?)

Karol Oleszek
  • 43
  • 1
  • 1
  • 4
  • It does not anwser my question. I want to have globally accesible functions, not a dictionary. – Karol Oleszek Dec 25 '17 at 20:29
  • 1
    That does answer your question. Using global dynamically created variables is a terrible idea. Make a dictionary, and stick the functions in the dictionary. You'll want to use `lambda` though, not `def`. – Carcigenicate Dec 25 '17 at 20:34

0 Answers0