0

I want to create functions (def $name:) where name comes from array=['str1', 'str2', ...].

I tried the code above and already searched on stackoverflow and google but did not find any solution.

array = ['String1', 'String2', ...]

def array[0]:

     code1

def array[1]:
     code2

String1()
String2()
Simon l.
  • 451
  • 1
  • 4
  • 4

1 Answers1

0

You can't define a function using a variable. But it turns out that you can rebind functions variable names.

See this post: How to use a variable as function name in Python

Example of how to do that:

one = 'one'
two = 'two'
three = 'three'
l = [one, two, three]
def some_stuff():
    print("i am sure some stuff")
for item in l:
    def _f():
        some_stuff()
    globals()[item] = _f
    del _f

one()
two()
three()
brazosFX
  • 342
  • 1
  • 11