Python Forum
List in function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: List in function (/thread-6380.html)



List in function - thehimanshukeshri - Nov-19-2017

def f(x,l=):
for i in range(x):
l.append(i*i)
print(l)

f(2)
f(3,[3,2,1])
f(3)


[0, 1]
[3, 2, 1, 0, 1, 4]
#How this is possible?
[0, 1, 0, 1, 4]
Please explain


RE: List in function - buran - Nov-19-2017

see
http://docs.python-guide.org/en/latest/writing/gotchas/
https://pythonconquerstheuniverse.wordpress.com/2012/02/15/mutable-default-arguments/