Decorators inside of class and decorator parameters

MR ptwobrussell at gmail.com
Sat Jan 13 17:56:30 EST 2007


Hello All,

I have a question about decorators, and I think an illustration would
be helpful. Consider the following simple class:

#begin code
class Foo:
    def fooDecorator(f):
        print "fooDecorator"

        def _f(self, *args, **kw):
            return f(self, *args, **kw)

        return _f

    @fooDecorator
    def fooMethod(self):
        print "fooMethod"

f = Foo()
f.fooMethod()
#end of code

This code runs, and actually serves my purpose. However, I'm a little
confused about three things and wanted to try and work through them
while I had the time to do so. I believe all of my confusion is related
to the parameters related to the fooDecorator:

-how I would pass arguments into the fooDecorator if I wanted to (my
various attempts have failed)
-what the difference is between decorating with @fooDecorator versus
@fooDecorator()
-why does this code even work, because the first argument to
fooDecorator isn't self

I'm searched the net and read the PEPs that seemed relevant, but I
didn't see much about decorators inside of a class like this. Can
anyone comment on any of these three things?




More information about the Python-list mailing list