0

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?

alvalentini
  • 145
  • 2
  • 5
  • Briefly: use a data structure like a list or dictionary. – TigerhawkT3 Dec 25 '16 at 02:58
  • @TigerhawkT3 I don't find it as a duplicate of one you mentioned. This person wants to define functions with different names. – Pulkit Goyal Dec 25 '16 at 03:44
  • 1
    @PulkitGoyal - And you do that by adding the function references to a list or dictionary. It is a perfect duplicate. It's simply `def myfunct(): pass # do stuff` and then `name = {c:myfunc for c in 'abcd'}`, at which point the desired reference of e.g. `a` is `name['a']`. If each function is supposed to be different, they can be defined as needed in the loop, adding the reference to the defined function to the dictionary. – TigerhawkT3 Dec 25 '16 at 05:34

0 Answers0