On 30 Dec, 17:26, George Sakkis <[EMAIL PROTECTED]> wrote: > On Dec 29, 9:14 pm, bukzor <[EMAIL PROTECTED]> wrote: > > > Here's the answer to the > > question:http://www.python.org/doc/faq/general/#why-are-default-values-shared-... > > > It looks like Guido disagrees with me, so the discussion is closed. > > Note that the FAQ mainly explains *what* happens, not *why* was this > decision taken. Although it shows an example where "this feature can > be useful", it's neither the only way to do it nor is memoization as > common as wanting fresh default arguments on every call. >
I'm surprised noone has said anything about the why of default mutables. I think it is becasue it isn't easy to do it an other way. def some_function( an_integer=1,pointless_list=[], random_fuction_value=random_function()): pass To you and me it is obvious that this is an integer, a list and a function call, but to python it is just 3 objects. Python'd have to check each argument carefully to determine if it is mutable or not. Or always copy each object, adding additional overhead to function calls, and making passing arguments to functions expensive. Even if these problems were solved, it would only make the problem less common, not extinct. # hypothetical def another_function( still_alive=([],) ): still_alive[0].append('spam') print still_alive >>> another_function() (['spam'],) >>> another_function() (['spam', 'spam'],) (Could of course be solved by always making deep copies of all arguments.) While I would welcome making mutable defaults work differently, I don't see any way to make such a change without making unacceptable tradeoffs. -- Incidentally, I wrote a program a while back, with a bug caused by mutable defaults. Never bothered to change it, it was the behaviour I wanted, just not the one I thought I had implemented. -- Python, so good even the bugs make the program better. -- http://mail.python.org/mailman/listinfo/python-list