[Python-ideas] Multiple arguments for decorators

Andrew Barnert abarnert at yahoo.com
Mon Nov 30 23:47:09 EST 2015


Tangent here:

Is this potentially a good use case for Nick Coghlan's PEP 403 (@in clauses) and/or PEP 3150 (statement-local namespaces with "given:")?

At first glance, the obvious way to use PEP 3150 doesn't look beautiful:

    class Spam:
        x = property(**?.__dict__) given:
            """x"""
            def fget(self): return self._x
            def fset(self, value): self._x = value
            def fdel(self): del self._x

But maybe there's a way to make this nicer?

> On Nov 30, 2015, at 20:37, Andrew Barnert via Python-ideas <python-ideas at python.org> wrote:
> 
>> On Nov 30, 2015, at 19:21, Kevin Modzelewski via Python-ideas <python-ideas at python.org> wrote:
>> 
>> Class scopes definitely feel like a good match -- they are a way of saying "evaluate all of these expression, pass the resulting locals to a custom function, and bind the result of that function to the classname".  Usually the function is type(), which constructs a new class, but by setting a custom metaclass we can avoid creating a class just to wrap the scope:
> 
> Is there really a harm in creating a class?
> 
> A property is a type, and the obvious way to simulate it in Python rather than C (as shown by the sample code in the HOWTO) is with a class statement.
> 
> Besides, if you're creating many thousands of properties in a loop, the time and space cost of property creation is probably the least of your worries.
> 
> Again, maybe that isn't true for other types of decorators this feature might be useful for, but without having any examples to think about, it's hard to guess...
> 
>> class PropertyMetaclass(type):
>>    def __new__(cls, name, bases, attrs):
>>        return property(attrs.get('get'), attrs.get('set'), attrs.get('del'), attrs.get('__doc__'))
> 
> I still don't get the benefit of having a metaclass or constructor function or wrapper function or anything else, instead of just making property take a class instead of four functions. The latter is significantly nicer on the user side, and only a tiny bit more verbose in the implementation of property, and easier to understand. Unless there are other decorators where they wouldn't be true, or so many potentially useful one-shot decorators that defining them all a little more succinctly is worth the cost, why add the extra layer?
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/


More information about the Python-ideas mailing list