473,508 Members | 2,247 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

decorator with keyword

I'm sure they have been mentioned somewhere but here are some more
advantages of a decorator keyword (I use "transform"):

- The docstring can be moved to the top of the decorator suite.
- Simple attributes that don't affect the function's operation directly can
be written in the "natural" name = value form.
- Though I expect them to be rare like they are in classes today, statements
like if __debug__: decorateForDebugging would be possible.

A docstring and a single decorator - the common case:

transform:
""" Factory for new decorator syntaxes.

Keeps all proposals in a list and will recombine
them at random if called without a spec. """
staticmethod
def makeDecoratorSyntax(spec=None):
raise NotImplementedException()

The same with a pie:

@staticmethod
def makeDecoratorSyntax(spec=None):
""" Factory for new decorator syntaxes.

Keeps all proposals in a list and will recombine
them at random if called without a spec. """
raise NotImplementedException()

I'd say no clear winner here. Now a heavily decorated function:

transform:
"""This method blah, blah.

It supports the following arguments:
- longArgumentOne -- a string giving ...
- longArgumentTwo -- a number giving ...

blah, blah.

"""
author = "BDFL"
status = "experimental"
grammar = "'@' dotted_name [ '(' [arglist] ')' ]"
staticmethod
def longMethodNameForEffect(longArgumentOne=None,
longArgumentTwo=42):
if longArgumentOne is None:
longArgumentOne = setDefault(longArgumentTwo)
for line in longArgumentOne:
if not isBogus(line):
print line

The same with pies:

@funcattrs(author="BDFL", status="experimental",
grammar="'@' dotted_name [ '(' [arglist] ')' ]")
@staticmethod
def longMethodNameForEffect(longArgumentOne=None,
longArgumentTwo=42):
"""This method blah, blah.

It supports the following arguments:
- longArgumentOne -- a string giving ...
- longArgumentTwo -- a number giving ...

blah, blah.

"""
if longArgumentOne is None:
longArgumentOne = setDefault(longArgumentTwo)
for line in longArgumentOne:
if not isBogus(line):
print line

A long docstring can indeed tear apart signature and implementation.

For the sake of completeness, a plain old function:

def filter(cond, seq):
"""filter(function or None, sequence) -> list, tuple, or string

Return those items of sequence for which function(item) is true. If
function is None, return the items that are true. If sequence is a
tuple or string, return the same type, else return a list."""

if cond is None:
cond = bool
return [item for item in seq if cond(item)]

transform:
"""filter(function or None, sequence) -> list, tuple, or string

Return those items of sequence for which function(item) is true. If
function is None, return the items that are true. If sequence is a
tuple or string, return the same type, else return a list."""
def filter(cond, seq):
if cond is None:
cond = bool
return [item for item in seq if cond(item)]

"transform" looks a bit pathetic for a docstring, but otherwise I'd say the
grouping might even be slighly clearer. Note how the function signature is
duplicated in the docstring taken from 2.3's filter() - that helps a lot
for long decoration suites.

The decoration suite would generate a list of (name, value) tuples which are
applied to the function like so

trafos = [("__doc__", "This method..."), ("author", "BDFL"), ..., (None,
staticmethod)]
trafos.reverse()
for name, value in trafos:
if name:
setattr(func, name, value)
else:
func = value(func)

I think I would even prefer something like the above over the current
classdict passed to metaclasses, i. e. ordering information and "unnamed
attributes" could be useful in classes, too.

Peter

Jul 18 '05 #1
7 1518
On Thu, 12 Aug 2004, Peter Otten wrote:
[decorator examples]


+2 on this (can I give a +2?). It's not only pretty, but it addresses my
gripe about decorators being used for too many purposes (by providing a
clean way to supply function attributes). Additionally, by moving
docstrings into the transform: block, this provides an easy way to
document the generated function, rather than forcing decorators to
preserve docstrings by copying the decoratee's docstring into the
decorated function.

The PythonDecorators wiki doesn't say Guido has specifically shot this
style down, so it may yet have a chance.

Jul 18 '05 #2
Christopher T King wrote:

[about the "decorate:" syntax]
The PythonDecorators wiki doesn't say Guido has specifically shot this
style down, so it may yet have a chance.


The python-dev mailing list doesn't appear to have discussed this one
yet, but given that much of the, uh, "chatter" that I detect there
is about minor tweaking of the @pie syntax, I fear they are so not
likely to pay much attention to new syntaxes and it might be too
late to get much support for it amongst the core folks. (Guido
referred to @pie as "the humble @decorator" yesterday...)

-Peter
Jul 18 '05 #3
Christopher T King wrote:
On Thu, 12 Aug 2004, Peter Otten wrote:
[decorator examples]


+2 on this (can I give a +2?). It's not only pretty, but it addresses my
gripe about decorators being used for too many purposes (by providing a
clean way to supply function attributes). Additionally, by moving
docstrings into the transform: block, this provides an easy way to
document the generated function, rather than forcing decorators to
preserve docstrings by copying the decoratee's docstring into the
decorated function.

The PythonDecorators wiki doesn't say Guido has specifically shot this
style down, so it may yet have a chance.


I would also support this variant. I was a supporter of list-after-def,
but this seems to me pythonic AND readable.

+1! Hope Guido hears about that proposal!

Reinhold

--
Wenn eine Linuxdistribution so wenig brauchbare Software wie Windows
mitbrächte, wäre das bedauerlich. Was bei Windows der Umfang eines
"kompletten Betriebssystems" ist, nennt man bei Linux eine Rescuedisk.
-- David Kastrup in de.comp.os.unix.linux.misc
Jul 18 '05 #4
Peter Hansen wrote:
Christopher T King wrote:

[about the "decorate:" syntax]
The PythonDecorators wiki doesn't say Guido has specifically shot this
style down, so it may yet have a chance.


The python-dev mailing list doesn't appear to have discussed this one
yet, but given that much of the, uh, "chatter" that I detect there
is about minor tweaking of the @pie syntax, I fear they are so not
likely to pay much attention to new syntaxes and it might be too
late to get much support for it amongst the core folks. (Guido
referred to @pie as "the humble @decorator" yesterday...)


I'm sure it would help if every supporter of this syntax started a
thread of his own on python-dev *wink*

Reinhold

--
Wenn eine Linuxdistribution so wenig brauchbare Software wie Windows
mitbrächte, wäre das bedauerlich. Was bei Windows der Umfang eines
"kompletten Betriebssystems" ist, nennt man bei Linux eine Rescuedisk.
-- David Kastrup in de.comp.os.unix.linux.misc
Jul 18 '05 #5
Reinhold Birkenfeld wrote:
Peter Hansen wrote:
The python-dev mailing list doesn't appear to have discussed this one
yet, but given that much of the, uh, "chatter" that I detect there
is about minor tweaking of the @pie syntax, I fear they are so not
likely to pay much attention to new syntaxes and it might be too
late to get much support for it amongst the core folks. (Guido
referred to @pie as "the humble @decorator" yesterday...)


I'm sure it would help if every supporter of this syntax started a
thread of his own on python-dev *wink*


Glad you put the wink in... I doubt that would help the
cause.

The syntax should be presented there, perhaps, preferably
by someone calm and reasonable (i.e. not me), but I think
the most valuable thing is to continue discussion here
until it's apparent (if that's going to happen) that there
is a fairly wide consensus that decorate: (possibly with
a different keyword) is far preferred to @pie... At least,
I think that was roughly what was asked for of the masses,
if the masses had anything to say about the matter.

-Peter
Jul 18 '05 #6
Peter Hansen wrote:
Reinhold Birkenfeld wrote:
Peter Hansen wrote:
The python-dev mailing list doesn't appear to have discussed this one
yet, but given that much of the, uh, "chatter" that I detect there
is about minor tweaking of the @pie syntax, I fear they are so not
likely to pay much attention to new syntaxes and it might be too
late to get much support for it amongst the core folks. (Guido
referred to @pie as "the humble @decorator" yesterday...)


I'm sure it would help if every supporter of this syntax started a
thread of his own on python-dev *wink*


Glad you put the wink in... I doubt that would help the
cause.

The syntax should be presented there, perhaps, preferably
by someone calm and reasonable (i.e. not me), but I think
the most valuable thing is to continue discussion here
until it's apparent (if that's going to happen) that there
is a fairly wide consensus that decorate: (possibly with
a different keyword) is far preferred to @pie... At least,
I think that was roughly what was asked for of the masses,
if the masses had anything to say about the matter.


Full ACK!

Reinhold

--
Wenn eine Linuxdistribution so wenig brauchbare Software wie Windows
mitbrächte, wäre das bedauerlich. Was bei Windows der Umfang eines
"kompletten Betriebssystems" ist, nennt man bei Linux eine Rescuedisk.
-- David Kastrup in de.comp.os.unix.linux.misc
Jul 18 '05 #7
On Thu, 12 Aug 2004 23:03:39 -0400, Peter Hansen <pe***@engcorp.com> wrote:
Reinhold Birkenfeld wrote:
I'm sure it would help if every supporter of this syntax started a
thread of his own on python-dev *wink* Glad you put the wink in... I doubt that would help the
cause.


And Brett Cannon would probably have you whacked if you did it.

that there
is a fairly wide consensus that decorate: (possibly with
a different keyword) is far preferred to @pie...
FWIW, I still prefer pie-decorators to decorate. That's just
from translating my trusty decorator-testbed-code into it.
At least,
I think that was roughly what was asked for of the masses,
if the masses had anything to say about the matter.


Pretty much - although the theory was that there would need
to be a discussion on technical merits of one over the other,
rather than "here's a list of people who prefer this or that".
But you knew that already...
Jul 18 '05 #8

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

Similar topics

14
2280
by: Sandy Norton | last post by:
If we are going to be stuck with @decorators for 2.4, then how about using blocks and indentation to elminate repetition and increase readability: Example 1 --------- class Klass: def...
3
1323
by: Steven Bethard | last post by:
I mentioned in a previous post that I'd much prefer some sort of keyword as a decorator indication than a character like @ (or the recently suggested |). A promising note on python-dev: ...
24
2027
by: Steven Bethard | last post by:
I think one of the biggest reasons we're having such problems coming to any agreement on decorator syntax is that each proposal makes a number of syntax decisions, not just one. For decorators, I...
11
1613
by: Ville Vainio | last post by:
It might just be that @decorator might not be all that bad. When you look at code that uses it it's not that ugly after all. A lot of the furor about this is probably because it happened so...
7
1573
by: Steven Bethard | last post by:
So here's the state of the decorator debate as I see it: *** Location GvR pretty strongly wants decorators before the function: ...
41
2811
by: John Marshall | last post by:
How about the following, which I am almost positive has not been suggested: ----- class Klass: def __init__(self, name): self.name = name deco meth0: staticmethod def meth0(x):
13
1842
by: Paul McGuire | last post by:
Thanks to everyone who has voted so far - please keep them coming! Lurkers, this means you! In the interests of saving time, I propose that an additional thread start soon, to determine choices...
17
1692
by: Jim Jewett | last post by:
Guido has said that he is open to considering *one* alternative decorator syntax. At the moment, (Phillip Eby's suggestion) J4 <URL: http://www.python.org/moin/PythonDecorators > (section 5.21...
0
1246
by: Hallvard B Furuseth | last post by:
I haven't managed to catch up with all the decorator articles, but: If we do get the pie syntax (or | or %% or whatever), is it too late to get '@<keyword> staticmethod' and not just...
0
7129
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7333
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7398
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7061
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5637
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.