- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- How do I create a Python namespace?
- How can we create recursive functions in Python?
- How do I create a namespace package in Python?
- How to create python namespace packages in Python 3?
- Time Functions in Python?
- Recursive function to do substring search in C++
- How to write a function to get the time spent in each function in Python?
- How do I cache method calls in Python?
- How to write a recursive function in Python?
- How do I import all the submodules of a Python namespace package?
- Using a recursive function to capitalize each word in an array in JavaScript
- How do I declare a namespace in JavaScript?
- Why do some Python functions have underscores "__" before and after the function name?
- Spread operator in function calls JavaScript
- What is a namespace in Python?

Advertisements