[Python-Dev] method decorators (PEP 318)

Jewett, Jim J jim.jewett at eds.com
Fri Mar 26 16:17:05 EST 2004


Guido van Rossum:

> ... for decorators spelled with a single word, like classmethod,
> ... as early as reasonable in the function definition

>    def foobar(cls, blooh, blah) [classmethod]:

> hides a more important fact for understanding it (classmethod) behind
> some less important facts (the argument list).  I would much rather
> see this:

>    def foobar [classmethod] (cls, blooh, blah):

One alternative placed it earlier still, in a second header clause.

    using:
        classmethod
    def foobar(cls, blooh, blah):
        pass

This isn't as good for single short decorators, but it does scale up
better than anything which adds directly to the def clause.

    using:
        metadata(author="GvR",
                 version="1.0",
                 copyright="PSF",
                 ...)
        deprecated
    def foobar (self, blooh, blah):
        for bl, oh in blooh:
            print oh(blah(bl))

        for bl, oh in blooh:
            print oh(blah(bl))

It still risks hiding a few transformations in a large group
of annotations.

Using both the header and the suite creates more separation,
but it is an option.

    using classmethod:
        deprecated        
        metadata(author="GvR",
                 version="1.0",
                 copyright="PSF",
                 ...)
    def foobar (cls, blooh, blah):
        for bl, oh in blooh:
            print oh(blah(bl))

> I want to reserve the leading dot for attribute assignment to a
> special object specified by a 'with' statement, e.g.

>    with self:
>        .foo = [1, 2, 3]
>        .bar(4, .foo)

Would the about-to-be-defined object be available when specifying 
that special object?   (I've used "this" to separate it from the
enclosing class that "self" would refer to.)

    with this:
        .note = [1,2,3]
    def foo(args):
        pass

or even

    with proxy_dict(this):
        .note = [1,2,3]
    def foo(args):
        pass

How important is the annotation/transformation distinction?

    with this:
        .note = [1,2,3]
    using:
        classmethod
    def foo(args):
        pass

Phillip J. Eby:

> Okay, then how about:

>     def foobar(cls,blooh, blah):
>         [classmethod]
>         """This is a class method"""
>         # body

The above is already legal syntax, it just doesn't do much.  It 
does keep the """ """ from being a docstring, which in turn keeps 
it from being optimized out.

    def foobar(args):
        [1,2,3]
        """Here's proof you stole my code in a careless manner!"""
        pass

Breaking that particular usage might be amusing, but I'm not so 
happy about having to look farther and farther ahead to guess
where the magic ends.  @classmethod at least says "This part is
still magic."



More information about the Python-Dev mailing list