[Python-Dev] method decorators (PEP 318)

Skip Montanaro skip at pobox.com
Fri Mar 26 12:32:41 EST 2004


    Guido> (1) Put decorators of the first category early in the def
    Guido>     statement, for example here:

    Guido>     def foobar [classmethod] (cls, foo, bar):
    Guido>         ...

How is this better than afterwards?

    Guido> (2) Invent some other notation for setting function attributes as
    Guido>     part of the function *body*, before the doc string even.

    Guido> For (2) I am thinking aloud here:

    Guido>    def foobar(self, arg):
    Guido>        @author: "Guido van Rossum"
    Guido>        @deprecated
    Guido>        pass

It's a shame that there's no good way to define function attributes already.
Aside from the fact that this is different than the other form of
"decoration", it's also different than setting attributes for classes.
Rather than invent a unique syntax I'd prefer to either use a decorator
function or suffer with tacking them on at the end:

    def foobar(self, arg):
        pass
    foobar.author = "Guido van Rossum"
    foobar.deprecated = True

Using the proposed decorator syntax with the decorator after the arglist
with a little judicious indentation I can make it look sort of like what
you're after:

    def foobar(self, arg) [attributes(
        author = "Guido van Rossum"
        deprecated = True
        )]:
        pass

Another possibility would be to overload def:

    def foobar(self, arg):
        def author = "Guido van Rossum"
        def deprecated = True
        pass

I don't know if the parser can handle the lookahead necessary for that
though.

Skip



More information about the Python-Dev mailing list