0

I have a list of intended function names e.g. ['check_one', 'check_two', 'check_three'] and would like to create Python functions using them as function names.

The following doesn't work:

for func_name in ['check_one', 'check_two', 'check_three']:
    def f'{func_name}'(text):
        print(text)

The intended effect is that I have three functions defined as follows:

check_one('one fine day') # This function checks for the presence of the exact word 
                          # 'one' in the argument. It should return integer 1 if True, 
                          # and 0 if False. An example of a False result would be 'oneone fine day'.
check_two('hello world')  # Likewise, this function checks for the presence of the word 
                          # 'two' in the argument. In this case, it should return a 0.
check_three('c')          # Likewise

My original function is:

def check_one(string):
    return int((' ' + 'one' + ' ') in (' ' + string + ' '))

So I'd like a way to generate these functions through a loop, by providing a list of function names ['check_one', 'check_two', 'check_three'].

Would appreciate any help. Thank you!

xhalex
  • 1
  • 1
  • 2
  • 2
    Does this answer your question? [Programmatically create function specification](https://stackoverflow.com/questions/26987418/programmatically-create-function-specification) – Taxel Mar 25 '21 at 16:04
  • 2
    This looks like [an XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What is the **actual** problem that you are trying to solve? – Thierry Lathuille Mar 25 '21 at 16:08

2 Answers2

3

If you have something more complex than a lambda can represent, you'd probably want to make a function that creates/returns new functions, and then use a process similar to what @Samwise's answer describes:

def create_function():
    def f(text):
        print(text)
    return f

functions = {
    func_name: create_function()
    for func_name in ['check_one', 'check_two', 'check_three']
}

functions['check_one']("Hello World!")

# to get them out of a dict and into the local namespace, 
# update the local namespace directly
locals().update(functions)
check_one("Hello World!")

Depending on what you need to attach the functions to (e.g. a class or module) you can use the appropriate methods to insert the functions into that object's personal namespace instead of the runtime local namespace, replacing locals().update() accordingly.

Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53
0

Put them in a dict:

>>> functions = {
...     func_name: lambda text: print(text)
...     for func_name in ['check_one', 'check_two', 'check_three']
... }
>>>
>>> functions["check_one"]("Hello World!")
Hello World!
Samwise
  • 68,105
  • 3
  • 30
  • 44
  • I don't think this is answering the OP question as he wants to be able to call them as usual functions. – Selnay Mar 25 '21 at 16:07
  • There's nothing "unusual" about these functions. – Samwise Mar 25 '21 at 16:53
  • I was referring to the fact that the OP wants to call the functions like this: `check_one()` not like this: `functions["check_one"]()` – Selnay Mar 25 '21 at 16:54