'Multiple functions with optional arguments causes the arguments to get lost on the stack

The following code:

def func_3(some_number, other_number=1):
    print("func_3 " + str(other_number))
    return other_number
    
def func_2(some_number, other_number=1):
    print("func_2 " + str(other_number))
    return func_3(some_number, other_number=1)

def func_1(some_number, other_number=1):
    print("func_1 " + str(other_number))
    return func_2(some_number, other_number=1)

def func_0(some_number, other_number=1):
    print("func_0 " + str(other_number))
    return func_1(some_number, other_number=1)

func_0(123456, 2)

will generate the following output:

func_0 2
func_1 1
func_2 1
func_3 1

Why does this occur?



Solution 1:[1]

Besides func_0, you're explicitly passing in 1 for the other_number parameter. If you want to pass in 2, rather than 1, change the return statements so that they explicitly pass in other_number as a parameter.

For example, use:

return func_1(some_number, other_number=other_number)

rather than:

return func_1(some_number, other_number=1)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BrokenBenchmark