0
def make(x,y=[]):
    y.append(x)
    print(y)

for i in [5,8,20,2]:
    if i not in [10,20,30]:
        make(i)
    else:
        make(i,["A","B","C"])

Isn't the y list inside make() supposed to be disposed of by the interpreter after the code int the function gets executed?

Because when I run this, I get:

[5]
[5,8]
[A,B,C,20]
[5,8,2]

I was expecting the output to look like:

[5]
[8]
[A,B,C,20]
[2]

Can someone explain this to me?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Hero Yu
  • 11
  • 1
  • I like to ask a similar question in interview settings. I've also been asked a question related to this in an interview before as well. – rv.kvetch Sep 06 '22 at 18:06

0 Answers0