[Python-Dev] Re: new syntax for wrapping (PEP 318)

Barry Warsaw barry at python.org
Wed Feb 25 22:39:42 EST 2004


On Wed, 2004-02-25 at 21:56, Josiah Carlson wrote:

> I personally don't see the point of using any syntax for wrapping a
> function like so:
> 
> class a:
>     def funct(self):
>         pass
>     a = classmethod(a)

In a short example like this, you're right that it may not help much. 
But I've written a bunch of code that uses staticmethod or
transformations of (some) methods in a class.  When your method is three
quarters of a page long, the separation between the function definition
and the decoration is a readability burden.  What I tend to do in that
case is name the function with an underscore and use the 'public' name
for th wrapped attribute.  This is still less readable that it should be
because that little "foo = wrapper(_foo)" line can easily get lost.

I've rewritten some of my code to use both the [] and the 'as' syntax. 
Neither is ideal, primarily because the interesting information tends to
get pushed way off to the right, where I think the eye is lazier because
of the more ragged nature of "code right".

Putting the decorator between the def and the method name actually
doesn't look so bad to me, e.g.:

def [classmethod] func(a, b, c):

but I think with a longer decorator list, it might be problematic.  I
think that one element decorator lists will be the most common use and
in that case, I like this syntax because it almost reads like English. 
E.g. "define a classmethod called func taking arguments a, b, and c".

> Honesly, a new (or used) keyword would make the most sense, perhaps "as",
> which has been offered before.  At least then people could check the
> documentation index for "as" and get two listings, one for import
> statements, and one for decorator syntax.  

I agree with whoever said that people aren't going to look up 'as', they
are going to look up 'def' when trying to understand what this stuff
means.

> Even though I think the above is the best syntax I've seen so far, that
> doesn't mean that I like it.  Personally, I don't have any use for
> decorators currently, and could see new and old users of Python being
> stymied by examples of this in new code.  

Actually stuff 'like this' makes for some much more elegant Python
idioms.  I think the features are just so new that it hasn't really made
its way into much released production code.

I think Python 2.4 is the right time frame for improving the utility of
this very neat feature.

-Barry





More information about the Python-Dev mailing list