Do recursive functions in Python create a new namespace each time the function calls itself?


Yes, a function call (any function call, not just recursive ones) creates a new namespace. BUT, when given as parameters, OBJECTS are passed by reference.

So, the new namespace get its own copy of this reference but it still refers to the same object as in the calling function, and if you change the content of that object, you will notice the change in the calling function.

To be more specific, Whenever the Interpreter encounters a call to a function, its creates a frame object, which is pushed to a frame stack. Each time a frame is created, that frame is given its own private namespace, where each variable in the frame is re-defined.

Updated on: 30-Jul-2019

109 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements