[Python-ideas] Before and after the colon in funciton defs.

David Townshend aquavitae69 at gmail.com
Mon Sep 19 09:44:47 CEST 2011


I think something similar could be achieved with annotations, as in
the following example.

This is incomplete, and probably full of errors, but it gives the idea.

def setlocals(func):
    def wrapper(*args, **kwargs):
        call_kwargs = []
        for k, v in kwargs.items():
            if k in func.__annotations__ and func.__annotations__[k] == 'local':
                raise AttributeError("Can't set argument")
            else:
                call_kwargs[k] = v
        return func(*args, **call_kwargs)

>>> @setlocals
... def myfunc(a, b, loc=4:'local'):
...     print(loc)
>>> myfunc(2, 3)
4
>>> myfunc(2, 3, loc=5)
AttributeError



On Sep 18, 2011 10:59 PM, "Ron Adam" <ron3200 at gmail.com> wrote:
> 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
>
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas



More information about the Python-ideas mailing list