[Python-ideas] why is there no decoration for objects

Andrew Barnert abarnert at yahoo.com
Fri Aug 21 06:35:00 CEST 2015


On Aug 20, 2015, at 19:32, shiva prasanth <kesavarapu.siva at gmail.com> wrote:
> 
> clearly 
> we write decorators for classes and functions and now why not for objects

We use decorators for class definition statements and function definition statements. If you already have a class object or a function object lying around, you just say "d = decorator(f)". The only reason to use "@decorator" is to put the decoration at the top of the statement instead of way down after the body. And what the decorator does is to change what gets bound to the name in the definition statement.

There's no such thing as an "object declaration". I suppose an assignment statement is sort of similar, so you could I guess make sense of something like this:

    @increment
    a = 3

... which would do the same as:

    a = increment(3)

But what would be the point? The problem decorators are meant to solve is that class and function definitions are long, multi-line suites; that isn't a problem for assignment statements, which only have a simple expression, not a suite.

Meanwhile, for anything but assignment statements, there is no binding going on, so there's nothing for a decorator to do.

> suppose
> def increment(a):
>       return a+1
> a=5
> @increment
> a
> should modify the value of a to 6

So what should this do:

    @increment
    a+1

... or any other expression statement?

> but instead it is giving an error
> i think we should add decorators for objects also
> since when you are reviewing the same code 
> @increment is easier to understand than 
> a=a+1



More information about the Python-ideas mailing list