473,326 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

Oct 18 '05 #1
1 1581
> @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
Oct 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

68
by: Marco Bubke | last post by:
Hi I have read some mail on the dev mailing list about PEP 318 and find the new Syntax really ugly. def foo(x, y): pass I call this foo(1, 2), this isn't really intuitive to me! Also I...
45
by: Edward K. Ream | last post by:
Hello all, First of all, my present state of mind re pep 318 is one of sheepish confusion. I suspect pep 318 will not affect Leo significantly, but I am most surprised that an apparently...
3
by: Hallvard B Furuseth | last post by:
....and opinions:-) I don't understand some of the arguments in pep-318 (version 1.19), or what the current syntax has to do with some of the design goals mentioned there. Could someone...
11
by: Arien Malec | last post by:
I've been following the decorator debate with some interest, and it's taken me a reasonably long time to understand what is meant by a decorator. One of the issues is that the Decorator pattern is...
0
by: Anthony Baxter | last post by:
To go along with the 2.4a3 release, here's an updated version of the decorator PEP. It describes the state of decorators as they are in 2.4a3. PEP: 318 Title: Decorators for Functions and...
27
by: Brian Sabbey | last post by:
Here is a first draft of a PEP for thunks. Please let me know what you think. If there is a positive response, I will create a real PEP. I made a patch that implements thunks as described here....
19
by: Nicolas Fleury | last post by:
Hi everyone, I would to know what do you think of this PEP. Any comment welcomed (even about English mistakes). PEP: XXX Title: Specialization Syntax Version: $Revision: 1.10 $...
4
by: Tony Lownds | last post by:
(Note: PEPs in the 3xxx number range are intended for Python 3000) PEP: 3107 Title: Function Annotations Version: $Revision: 53169 $ Last-Modified: $Date: 2006-12-27 20:59:16 -0800 (Wed, 27 Dec...
81
by: Russ | last post by:
I just stumbled onto PEP 316: Programming by Contract for Python (http://www.python.org/dev/peps/pep-0316/). This would be a great addition to Python, but I see that it was submitted way back in...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.