PEP: Adding decorators for everything

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tharaka

    #1

    PEP: Adding decorators for everything

    Hi Guys,

    This is an idea for a PEP.

    How would you guys feel about adding decorator support for
    "everything "? Currently, only functions and method are supported.

    For example:

    @GuardedClass
    class Foo:
    @Transient
    a = 'a transient field, ignored when serializing'

    @Const
    PI = 22.0 / 7

    @TypeSafe(int)
    count = 10

    ...



    instead of:
    class Foo:
    a = Transient('a transient field, ignored when serializing')

    PI = Const(22.0 / 7)

    count = TypeSafe(int)(1 0)

    ...
    Foo = GuardedClass(Fo o)


    I mean, this would pave the way for a declarative style of programming
    (in a more intuitive way).

    It would also be better if multiple decorators could be written on the
    same line. E.g.:
    @A @B(x, y) @C
    def foo(): ...

    instead of
    @A
    @B(x, y)
    @C
    def foo(): ...

    (The function definition should start on the next line though).


    Suggestions, Comments, flames, anybody?


    Cheers!

  • Diez B. Roggisch

    #2
    Re: PEP: Adding decorators for everything

    > @GuardedClass[color=blue]
    > class Foo:[/color]

    The functionality can be done using a meta-class, in a similarily
    declarative way.
    [color=blue]
    > @Transient
    > a = 'a transient field, ignored when serializing'
    >
    > @Const
    > PI = 22.0 / 7
    >
    > @TypeSafe(int)
    > count = 10[/color]

    These are tricky, as the implicitly change the nature of the values -
    they become properties. And the decorator protocol has to change, as the
    passed value is obviously not a callable, but a random value. So in the
    end, you could simply do something like this:


    @Const(3.24)
    def PI(self):
    pass

    with Const basically ignoring its callable-argument and simply returning
    a get-only-property. I have to admit that I was tempted to use such a
    thingy just the other day. But it is not exactly nice, and using

    PI = Const(3.14) as you suggested is even more pleasing.

    Additionally, the first @Transient-decorator can't be done that way, as
    the decorator protocol doesn't know about the _name_ a thing is bound to
    later. And you'd need that to actually set up e.g. __getstate__ operate
    properly.

    And it doesn't mkae much sense anyway, as "a" is a class variable, not a
    instance variable.

    So - I'm not very much in favour of these enhancements.

    [color=blue]
    > It would also be better if multiple decorators could be written on the
    > same line. E.g.:
    > @A @B(x, y) @C
    > def foo(): ...[/color]

    That one I like.

    Diez

    Comment

    Working...