decorator with keyword

P

Peter Otten

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
 
C

Christopher T King

[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.
 
P

Peter Hansen

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
 
R

Reinhold Birkenfeld

Christopher said:
[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
 
R

Reinhold Birkenfeld

Peter said:
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
 
P

Peter Hansen

Reinhold said:
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
 
R

Reinhold Birkenfeld

Peter said:
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
 
A

Anthony Baxter

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...
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top