On Sat, 16 May 2009 01:52:44 am Curt Hagenlocher wrote:
There seem to be two separate "wants" that relate to this topic:
1. Preventing the "noob" mistake of saying "def f(x = {})" and expecting that a new empty dictionary will be produced for each call, and 2. Creating a more concise syntax for saying def f(x = UNDEF): if x is UNDEF: x = {}
So far, the discussion seems to have revolved entirely around the second request -- which I find by far less compelling than the first; it's simply not a painful-enough pattern to warrant a special bit of syntax. Furthermore, it doesn't do anything to address the first desire.
Agreed. Sort of. I don't believe that we can do anything about #1. Changing the current behaviour of default arguments will almost certainly not happen -- I think Guido has ruled No on that one, although I might be mistaken. But even if it was changed, it would just lead to a different set of newbie mistakes. I personally don't find the boilerplate code in #2 particularly onerous, but it *is* boilerplate, and as a general rule, boilerplate is a bad thing. If we could agree on a syntax, then we could move that boilerplate out of our code (where it marginally complicates the structure of the function), into the bytecode, which would be a small but valuable win for readability. There's also a #3: one possible solution to this is to use thunks. Do thunks have uses outside of default arguments? I imagine they do -- Algol used them extensively. What else can thunks be used for? -- Steven D'Aprano