[Python-3000] Assignment decorators
Jack Diederich
jackdiedk at jackdied.com
Fri Dec 8 23:23:53 CET 2006
On Fri, Dec 08, 2006 at 11:58:38AM -0800, Brett Cannon wrote:
> On 12/8/06, Talin <talin at acm.org> wrote:
> >
> >One other minor brainstorm before I head off to work: I'd like function
> >decorators to work with assignment statements as well as 'def' statements.
> >
> >Use case:
> >
> > class Foo:
> >
> > @private
> > def myfunc():
> > pass
> >
> > @private
> > selected = True
> >
> >Rationale: The 'def' statement is a type of assignment statement, in
> >that it assigns a value (a function object) to a symbol. For
> >consistency, the same mechanic ought to be available to other kinds of
> >assignment.
>
>
> That just seems ugly. Decorators are fine for functions, methods (and
> eventually classes) because they only appear in one place. But having them
> strewn about within a function body just seems messy to me.::
>
> def fxn():
> @decorator
> a = b
> @decorator
> c = d
> @decorator
> e = f
>
> Contrived, I know, but that just hurts my eyes. Plus if you want to change
> the value of something before it is assigned then pass the value to be
> assigned to a function. Decorators provide the ability to pre-process
> objects that one could not normally touch before assignment. You don't have
> this issue with assignment statements in a function and thus you are not
> solving a problem here that decorators were designed for.
Decorators let you announce that something is modified nearer to the
definition.
eg/
@munge
def foo():
# 50 lines
# instead of
def foo()
# 50 lines
foo = munge(foo)
Adding a decorator for simple assignments is at best benign and more likely
confusing because now you have to read two lines instead of one.
eg/
@munge
foo = 7
# instead of
foo = munge(7)
Function (and class) definitions are already spread out vertically and
decorators just change the order to put the more important parts first.
Simple assignments happen on one line so adding decorators would spread
out vertically what is an otherwise concise operation.
-Jack
More information about the Python-3000
mailing list