can i define a function inside a function in py code example

Example 1: python define a function within a function

def print_msg(msg): # This is the outer enclosing function
    
    def printer():# This is the nested function
        print(msg)

    return printer  # this got changed

# Now let's try calling this function.
# Output: Hello
another = print_msg("Hello")
another()

Example 2: nested functions in python

def print_msg(msg):
    # This is the outer enclosing function

    def printer():
        # This is the nested function
        print(msg)

    return printer  # returns the nested function


# Now let's try calling this function.
# Output: Hello
another = print_msg("Hello")
another()