Sobes.tech
Junior

Is it possible to declare a function inside another function and where will it be visible?

sobes.tech AI

Answer from AI

Yes, it is possible. An inner function (or nested function) is visible and accessible only within the outer function in which it is defined. Its scope is limited to the body of the outer function.

def outer_function():
    def inner_function(): # Nested function
        print("This is an inner function")

    inner_function() # Calling the inner function inside the outer function

outer_function() # Calling the outer function, which leads to executing the inner one
# inner_function() # This line will cause a NameError because inner_function is not visible outside outer_function
Is it possible to declare a function inside another… - sobes.tech