[Python-Dev] method decorators (PEP 318)

Robert Brewer fumanchu at amor.org
Fri Mar 26 12:57:54 EST 2004


Skip Montanaro wrote:
>     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

Agreed. There are enough mechanisms to alter an object's attribute dict.
I'd rather not introduce another one, preferring instead to stick to
"object.attr = value"--in this example, "foobar.deprecated = True".
Where that declaration goes is fungible.

> 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'd still prefer a dot. How about:

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

?

However, the idea of putting such attributes at the same level as the
rest of the function block is ugly to me--newbies are going to go mad
the first few times, trying to figure out *when* such statements are
evaluated. Such statements IMO need their own "container", whether
that's a block:

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

...or a list:

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

...or within the declaration:

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


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org



More information about the Python-Dev mailing list