473,804 Members | 2,122 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__: decorateForDebu gging 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 makeDecoratorSy ntax(spec=None) :
raise NotImplementedE xception()

The same with a pie:

@staticmethod
def makeDecoratorSy ntax(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 NotImplementedE xception()

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 = "experiment al"
grammar = "'@' dotted_name [ '(' [arglist] ')' ]"
staticmethod
def longMethodNameF orEffect(longAr gumentOne=None,
longArgumentTwo =42):
if longArgumentOne is None:
longArgumentOne = setDefault(long ArgumentTwo)
for line in longArgumentOne :
if not isBogus(line):
print line

The same with pies:

@funcattrs(auth or="BDFL", status="experim ental",
grammar="'@' dotted_name [ '(' [arglist] ')' ]")
@staticmethod
def longMethodNameF orEffect(longAr gumentOne=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(long ArgumentTwo)
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(funct ion 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(funct ion 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 1535
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 PythonDecorator s 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 PythonDecorator s 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 PythonDecorator s 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 Linuxdistributi on 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 PythonDecorator s 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 Linuxdistributi on 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 Linuxdistributi on 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
2319
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 __init__(self, name):
3
1340
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: http://mail.python.org/pipermail/python-dev/2004-August/047001.html > Perhaps this could be addressed by requiring "from __future__ import > decorators", for one release, just like was done for "yield". I > expect that this would be acceptable to the...
24
2073
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 see the following decisions that must be made: 1) Indicator Proposals differ on how some sort of indicator of "decoratorhood" is use. These include: * none (e.g. just the list, as in the "list-after-def" suggestion) * the '@' character
11
1638
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 quicly. The situation might have been different if we had seen a pronouncement a week before, in the vein of "I have chosen this syntax - it will go in to the next alpha". My chief worry was throwing away one of the few unused ascii symbols, but if...
7
1590
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: http://mail.python.org/pipermail/python-dev/2004-August/047112.html http://mail.python.org/pipermail/python-dev/2004-August/047279.html
41
2880
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
1871
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 for a decorator keyword, in the event that our selected syntax requires one. Please visit the PythonDecorators Wiki page, and navigate to section 6.1 Indicators (or follow this link...
17
1721
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 J4) looks very good to me -- and it is the only alternative without negatives. def func(arg1, arg2) @version("Added in 2.4") @returns(None)
0
1262
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 '@staticmethod'? I've suggested it before, and I think others have too, but it seems to have gotten lost in all the other discussion. It's supposed to be a plus with the current pie syntax that the @ suggests something 'new' and non-obvious is going on. ...
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10354
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10359
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10101
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6870
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3837
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.