Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Returning a new function from a function

As we can pass a function as a parameter to a function, we can also return a function from another one.

Combining it with the previouse example, iniside our create_function we define a new function. Normally it only exists inside the create_function, but we can return it to the caller and then it stays around.

def create_function():
    print("creating a function")
    def internal():
        print("This is the generated function")
    print("creation done")
    return internal

func = create_function()

func()



creating a function
creation done
This is the generated function