PEP: Adding decorators for everything

T

tharaka

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)(10)

...
Foo = GuardedClass(Foo)


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!
 
D

Diez B. Roggisch

@GuardedClass
class Foo:

The functionality can be done using a meta-class, in a similarily
declarative way.
@Transient
a = 'a transient field, ignored when serializing'

@Const
PI = 22.0 / 7

@TypeSafe(int)
count = 10

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.

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

That one I like.

Diez
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top