why is there no decoration for objects

clearly we write decorators for classes and functions and now why not for objects suppose def increment(a): return a+1 a=5 @increment a should modify the value of a to 6 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

On Fri, Aug 21, 2015 at 08:02:14AM +0530, shiva prasanth wrote:
I don't think it is easier to understand. Look at Stackoverflow or Reddit to see how many people ask "what's this funny @foo syntax mean?". Besides, you can write: a += 1 which is shorter than either alternative: # option 1 a = a + 1 # option 2 def increment(x): return x + 1 @increment a Decorators for functions and classes exist for a very good reason, which does not apply to simple variables. For them, just write: a = decorate(a) This puts the important thing (the decorator call) right there at the assignment. You can't miss it. But with a function or a class: def func(): # function header ... code more code lots of code func = decorate(func) the important thing (decorate) is lost, far from the function header. Using decorator syntax moves the important call to decorate to the top of the function, next to the header. There's no need for that with simple variables, since a regular function call is a one-liner and you can't miss it: a = decorate(a) -- Steve

On Aug 20, 2015, at 19:32, shiva prasanth <kesavarapu.siva@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.
So what should this do: @increment a+1 ... or any other expression statement?

On Fri, Aug 21, 2015 at 08:02:14AM +0530, shiva prasanth wrote:
I don't think it is easier to understand. Look at Stackoverflow or Reddit to see how many people ask "what's this funny @foo syntax mean?". Besides, you can write: a += 1 which is shorter than either alternative: # option 1 a = a + 1 # option 2 def increment(x): return x + 1 @increment a Decorators for functions and classes exist for a very good reason, which does not apply to simple variables. For them, just write: a = decorate(a) This puts the important thing (the decorator call) right there at the assignment. You can't miss it. But with a function or a class: def func(): # function header ... code more code lots of code func = decorate(func) the important thing (decorate) is lost, far from the function header. Using decorator syntax moves the important call to decorate to the top of the function, next to the header. There's no need for that with simple variables, since a regular function call is a one-liner and you can't miss it: a = decorate(a) -- Steve

On Aug 20, 2015, at 19:32, shiva prasanth <kesavarapu.siva@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.
So what should this do: @increment a+1 ... or any other expression statement?
participants (4)
-
Andrew Barnert
-
Joseph Jevnik
-
shiva prasanth
-
Steven D'Aprano