Something that confuses me in python

So given the following:
def f(x=[]):
    x.append(1)
    return x
print(f(),f())
The output is [1, 1] [1, 1]
So it seems like the function creates a global list which it appends to whenever it is called without parameters, and returns a reference to that single list. Can anyone explain this to me? Why isn't it local to the function, and recreated as an empty list for each call?

-- 
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : zakc93 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector

Reply via email to