[Python-ideas] Multiple arguments for decorators

Andrew Barnert abarnert at yahoo.com
Mon Nov 30 23:37:17 EST 2015


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?


More information about the Python-ideas mailing list