Dynamically defining functions using list elements in Python

27 views Asked by At

I'm trying to define a number of functions that I can then run concurrently using multiprocessing. In order to do this, I'm attempting to use a list of strings and iterate over the list, having each string be the name of a separate function.

Something similar to this:

nameList = ['a', 'b', 'c', 'd']
for name in nameList:
    def name():
        #do stuff

In the above example, I would ideally end up with four functions, a, b, c and d.

Clearly however, this doesn't work. Is there any way to achieve this?

0

There are 0 answers