call by objects problem - 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: call by objects problem (/thread-1624.html) |
call by objects problem - landlord1984 - Jan-17-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)Output: [0, 1] [3, 2, 1, 0, 1, 4] [0, 1, 0, 1, 4]I can understand the first two implementation of f(), but not the third. Can someone help explain why the third output starting with 0,1, which is the memory left by the first f() result? RE: call by objects problem - buran - Jan-17-2017 read this http://docs.python-guide.org/en/latest/writing/gotchas/ you should NOT use mutable default arguments |