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

Inner function created every time the outer function runs

Also defined during run-time, but in every call of bar() the innter_func is redefined again and again.

import random

def foo():
    return random.random()

print(foo())
print(foo())

def bar(a, b = foo()):

    def inner_func(x, y = foo()):
        return [x, y]

    print('inner', inner_func(a))
    return [a, b]

print(bar(1))
print(bar(2))

{% embed include file="src/examples/advanced-functions/runtime-inner-def.out)