[Python-ideas] Decorators for variables

Chris Angelico rosuav at gmail.com
Fri Apr 1 12:45:18 EDT 2016

On Sat, Apr 2, 2016 at 1:18 AM, Matthias welp <boekewurm at gmail.com> wrote:
> 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)

The whole point of decorator syntax for classes and functions is that
their definitions take many lines, and the decoration belongs as part
of the function signature or class definition. At the top of a
function block is a line which specifies the function name and any
arguments, and then you have the docstring. Similarly with classes -
name, superclasses, metaclass, docstring. All up the top. Placing the
decorator above that allows for an extremely convenient declarative
syntax that keeps all that information together. Also, the decorator
syntax replaces the redundant names:

def functionname():
functionname = decorator(functionname)

where the function first gets defined using its name, and then gets
rebound (which involves looking up the name and then assigning the
result back) - three separate uses of the name. In contrast, you're
asking for syntax to help you modify an expression. Expressions
already don't need the decorator syntax, because we can replace this:

var = some_value
var = decorator(var)

with this:

var = decorator(some_value)

as in your example. Decorator syntax buys us nothing above this.


More information about the Python-ideas mailing list