Dot operator magic has me stymied...

Discussion in 'Python' started by Casey Rodarmor, Oct 29, 2008.

  1. 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 "decorate.py", 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
    argument.

    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
     
    Casey Rodarmor, Oct 29, 2008
    #1
    1. Advertising

  2. On Oct 29, 7:46 pm, "Casey Rodarmor" <> wrote:
    > 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 "decorate.py", 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
    > argument.
    >
    > Can anybody shed some light on what's happening here?


    When

    @decorator
    def greet(self, name):
    print self.msg, name

    is executed, a new decorator object is created whose function
    attribute is the function 'greet' above (with two arguments) and the
    result is given the name 'greet'.

    Later, when

    >>> foo = Foo()
    >>> foo.greet("Bob")


    is executed,

    foo.greet is a decorator object whose attribute 'function' is the
    plain function 'greet'. So it expects two arguments and you only
    provide one.

    To fix this you could use the Descriptor protocol. If you don't know
    what it is, I suggest you read this:

    http://users.rcn.com/python/download/Descriptor.htm

    It should give you what you need to get it right.

    HTH

    --
    Arnaud
     
    Arnaud Delobelle, Oct 29, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Samuël van Laere

    To dot or not to dot?

    Samuël van Laere, Oct 16, 2003, in forum: HTML
    Replies:
    8
    Views:
    433
    Samuël van Laere
    Oct 16, 2003
  2. Christopher M. Lusardi

    volatile struct in dot h vs dot c

    Christopher M. Lusardi, May 11, 2004, in forum: C Programming
    Replies:
    3
    Views:
    482
    Peter Shaggy Haywood
    May 15, 2004
  3. Andrew
    Replies:
    10
    Views:
    699
    Neredbojias
    Oct 16, 2008
  4. Giles Bowkett
    Replies:
    9
    Views:
    414
    Giles Bowkett
    Dec 17, 2007
  5. Replies:
    6
    Views:
    256
    Thomas 'PointedEars' Lahn
    Dec 12, 2005
Loading...

Share This Page