[Python-ideas] Multiple arguments for decorators
Sven R. Kunze
srkunze at mail.de
Tue Dec 1 11:41:19 EST 2015
Nice idea and as concise as other languages do.
I would appreciate a stdlib-provided 'Property'. Meta classes always
make me feel that I need to bury them deeeeep down in some library. As
properties are considered standard repetory these days (as you mentioned
in the last post), I feel stdlib is the place.
Best,
Sven
On 01.12.2015 17:22, Guido van Rossum wrote:
> On the other hand, if we're willing to put up with some ugliness in
> the *implementation*, the *notation* can be fairly clean (and avoid
> the creation of a class object):
>
> class Example:
> def __init__(self):
> self._x = 0.0
> self._y = 0.0
>
> class x(Property):
> def get(self):
> return self._x
> def set(self, value):
> self._x = float(value)
>
> class y(Property):
> def get(self):
> return self._y
> def set(self, value):
> self._y = float(value)
>
> Notice there's no explicit mention of metaclasses here. The magic is
> that Property is a class with a custom metaclass. The implementation
> could be as simple as this:
>
> class MetaProperty(type):
> """Metaclass for Property below."""
>
> def __new__(cls, name, bases, attrs):
> if name == 'Property' and attrs['__module__'] == cls.__module__:
> # Defining the 'Property' class.
> return super().__new__(cls, name, bases, attrs)
> else:
> # Creating a property. Avoid creating a class at all.
> # Return a property instance.
> assert bases == (Property,)
> return property(attrs.get('get'), attrs.get('set'),
> attrs.get('delete'), attrs.get('__doc__'))
>
>
> class Property(metaclass=MetaProperty):
> """Inherit from this to define a read-write property."""
>
> --
> --Guido van Rossum (python.org/~guido <http://python.org/%7Eguido>)
>
>
> _______________________________________________
> 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/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20151201/a54b7704/attachment.html>
More information about the Python-ideas
mailing list