How to dynamically define functions?

Do you want to define these individually in your source file, statically? Then your best option would be to write a script to generate them.

If on the other hand you want these functions at runtime you can use a higher order function. For e.g.

>>> def make_func(value_to_print):
...     def _function():
...         print value_to_print
...     return _function
...
>>> f1 = make_func(1)
>>> f1()
1
>>> f2 = make_func(2)
>>> f2()
2

You can generate a list of these and store, again at runtime.

>>> my_functions = [make_func(i) for i in range(1, 11)]
>>> for each in my_functions:
...     each()
...
1
2
3
...

You may put new symbols into the dictionary of current variable bindings returned by vars():

for i in range(1, 11):
    def f(x):
        def g():
            print x
        return g
    vars()['activate_field_%d' % i] = f(i)

>>> activate_field_3()
3

But this trick is generally not recommented unless you definitely sure you need it.


Here's something that produces function names exactly like you wanted (and is a little simpler than the Dynamic/runtime method creation's accepted answer mentioned in @Goutham's now deleted answer):

FUNC_TEMPLATE = """def activate_field_{0}(): print({0})"""
for x in range(1, 11): exec(FUNC_TEMPLATE.format(x))

>>> activate_field_1()
1
>>> activate_field_7()
7

In Python versions 3.6+, it can be written as shown below using so-called f-string literals:

for x in range(1, 11): exec(f"""def activate_field_{x}(): print({x})""")

Tags:

Python