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 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.
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
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
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
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
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
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... This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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:
...
|
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...
|
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...
|
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:
...
| |
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):
|
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...
|
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...
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |