Dot operator magic has me stymied...

Casey Rodarmor caseyrodarmor at
Wed Oct 29 20:46:50 CET 2008

Hi All,

I'm trying to use a class as a decorator for another class method, but
it's giving me a lot of grief. Basically, my problem is with the
example below:

>>> class decorator:
...     def __init__(self, function):
...         self.function = function
...     def __call__(self, *args, **kwargs):
...         self.function(*args, **kwargs)
>>> class Foo:
...     def __init__(self):
...         self.msg = "Hello,"
...     @decorator
...     def greet(self, name):
...         print self.msg, name
>>> foo = Foo()
>>> foo.greet("Bob")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "", line 6, in __call__
    self.function(*args, **kwargs)
TypeError: greet() takes exactly 2 arguments (1 given)

I'm guessing that using a decorator that returns a class instance
instead of a function instance has messed up the magic of the dot
operator, causing it not to bind the foo instance to the self

Can anybody shed some light on what's happening here?

Also, I really do like using classes as decorators. Are there any
workarounds to get it to work with methods?

Thanks a bunch!

Best regards,
Casey Rodarmor

More information about the Python-list mailing list