[Python-ideas] Decorators for variables
Ian Kelly
ian.g.kelly at gmail.com
Fri Apr 1 10:37:32 EDT 2016
On Fri, Apr 1, 2016 at 8:18 AM, Matthias welp <boekewurm at gmail.com> wrote:
> 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.
I don't see the relationship between this paragraph and the rest of
your proposal. How does a decorator in place of an explicit function
call prevent repetition?
> I propose the following syntax, essentially equal to the syntax of function
> decorators:
>
> @decorator
> var = some_value
>
> which would be the same as
>
> var = decorator(some_value)
>
> and can be chained as well:
>
> @decorator
> @decorator_2
> var = some_value
>
> which would be
>
> var = decorator(decorator_2(some_value))
>
> or similarly
>
> var = decorator(decorator_2())
> var = some_value
What about augmented assignment? Should this work?
@float
var += 20
And would that be equivalent to:
@float
var = var + 20
Or:
var = var + float(20)
Also, what about attributes and items?
@decorator
x.attr = value
@another_decorator
d['foo'] = value
> The main idea behind the proposal is that you can use the decorator as a
> standardized way to create variables that have the same behaviour, instead
> of havng to do that using methods. I think that a lot can be gained by
> specifying a decorator that can decorate variables or properties.
By "methods" you mean "function composition", right? Otherwise I don't
understand what methods have got to do with this.
More information about the Python-ideas
mailing list