0

I've step across this behavior in python:

def a(fruit,fruit_list=['banana','apple','orange']):
    fruit_list.append(fruit)
    print(fruit_list)

a('cherry')
a('kiwi')

Output:

['banana', 'apple', 'orange', 'cherry']
['banana', 'apple', 'orange', 'cherry', 'kiwi']

I was wondering, are python parameters in functions saved in the stack as global parameters?

Slava Bugz
  • 318
  • 5
  • 17
  • 1
    The default for *fruit_list* is constructed once - i.e., when the function is initially parsed by the Python interpreter. It persists. That is to say that there is a single instance of that list. Its visibility is limited to the scope of the function. Any changes made to it will be permanent as you observe in your code. They are local to the function in which they are declared – DarkKnight Feb 02 '23 at 07:54

0 Answers0