On Sun, 2011-09-18 at 15:32 -0400, Jim Jewett wrote:
> On Sat, Sep 17, 2011 at 2:16 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
>> > Specifically targeting existing uses of the default argument hack, on
> > the other hand, comes with at least 3 already known use cases (i.e.
> > name lookup micro-optimisation, early binding semantics and shared
> > state for algorithms). People already tolerate messing with the
> > function signature to obtain those behaviours, so it's reasonable to
> > wonder if it is possible to provide a cleaner way to obtain the same
> > effect. A simple set of definition time name bindings would likely
> > suffice without greatly increasing the language complexity or the
> > runtime overhead of function calls.
>> Since the goal is to add some context, like a wrapper, it seems
> reasonable to use a specialization of @. @+ suggests adding something
> to the environment, and won't be ambiguous unless numbers become
> callable.
>> @+tuple=tuple
> @+sorted=sorted
> @+len=len
> @+KeyError=KeyError
> def decorating_function(user_function):
>> I would personally be fine with a restriction to @+name=<expr> but I
> suppose others might prefer a tuple, like
>>> @+(tuple, sorted, len, KeyError)=(tuple, sorted, len, KeyError)
> def decorating_function(user_function):
If there was a method on a function object to set default values, then
you could use a regular decorator.
# Most likely to live in functools
def preset(**kwds):
def apply(f):
f.set_default_values(kwds)
return f
return apply
#---------------------------------
from functools import preset
@preset(tuple=tuple, sorted=sorted, len=len, keyerror=keyerror)
def foo(...):
...
I was hoping there might be a more direct way to do it. My previous
idea breaks the white space consistency, and Nicks idea adds special
syntax. Decorators with long arguments make the function name stand out
less.
Is there a more recent patch for Pep 362?
Pep 362 seems to be targeted at only introspection and really isn't a
part of the function object. Is that correct?
Cheers,
Ron