[Python-ideas] Decorators for variables
Matthias welp
boekewurm at gmail.com
Fri Apr 1 11:46:26 EDT 2016
> > Currently the way to create variables with custom get/set/deleters is
to
> > use the @property decorator or use property(get, set, del, doc?), and
> > this must be repeated per variable. If I were able to decorate multiple
> > properties with decorators like @not_none or something similar, it
> > would take away a lot of currently used confusing code.
>
> How does this prevent repetition?
If you were to define a variable you currently could use the @property for
each variable, which could take up to 3 declarations of the same name per
use of the pattern. Using a decorator might take that down to only 1 extra
line.
> What about augmented assignment? Should this work?
The steps it would go through were these:
1. the value of the statement is calculated. e.g. val + 20 in the first
case given.
2. the decorator is applied on that value
3. the return value from the decorator is then assigned to the variable.
This is, again, very similar to the way function decorators work, and a
short-handed method to make property access more transparent to the
programmer.
> Also, what about attributes and items?
I have not yet thought about that, as they are not direct scope variables.
The idea was there to decorate the attribute or variable at the moment it
would get defined, not per se after definition.
> > instead of having to do that using methods
>
> By "methods" you mean "function composition", right?
Sorry for my terminology, I meant function calls, but function composition
is indeed what would happen effectively.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160401/7e6f59ac/attachment.html>
More information about the Python-ideas
mailing list