Larry Hastings wrote:
George Sakkis wrote:
+0.x for a new keyword that adds dynamic semantics (and removes the need for the sentinel kludge).
We don't need new syntax for it. Here's a proof-of-concept hack that you can do it with a function decorator.
Your decorator only works for mutables where you just want a deep copy. It doesn't work for cases where you want a whole expression to be re-evaluated from scratch. (Maybe for the side effects or something.) That said, it couldn't be that hard to work out a similar decorator using lambda thunks instead. The internals of the decorator would be something like:
for n, arg in enumerate(args): if arg is defaults[n]: #If you didn't get passed anything args[n] = defaults[n]() #Unthunk the lambda
The usage might be:
@dynamicdefaults def f(arg=lambda: dosomething()):
It cuts 3 lines of boilerplate down to one line, but makes all your function calls a little slower.