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

PythonServer Side ProgrammingProgramming

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.

raja
Published on 21-Dec-2017 10:48:24
Advertisements