This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Invalid TypeError with class method decorator and class method parameter
Type: behavior Stage:
Components: None Versions: Python 2.4, Python 2.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, tdimson
Priority: normal Keywords:

Created on 2008-04-02 02:23 by tdimson, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (2)
msg64830 - (view) Author: tdimson (tdimson) Date: 2008-04-02 02:23
Having a peculiar issue (exception raised despite being valid) when
defining a decorator that takes a class method as a callback. Here is a
cooked example:

def decorator( callback ):
    def inner(func):
        def application( *args, **kwargs ):
            callback(*args,**kwargs)
            func(*args,**kwargs)
        return application
    return inner

class DecorateMe:
    @decorator( callback=DecorateMe.callback )
    def youBet( self ):
        pass
    def callback( self ):
        print "Hello!"

>>> DecorateMe().youBet()
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    DecorateMe().youBet()
  File "C:\Python25\badpython.py", line 4, in application
    callback(*args,**kwargs)
TypeError: unbound method callback() must be called with DecorateMe
instance as first argument (got DecorateMe instance instead)

If you change the line "callback=DecorateMe.callback" to
"callback=lambda x: DecorateMe.callback(x)" python gives you:

>>> DecorateMe().youBet()
Hello!

Mysteriously, I did encounter this during my coding with a non-cooked
decorator.
msg64839 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2008-04-02 07:37
First, your code does not compile: when compiling the DecorateMe class
body, the DecorateMe class does not yet exist, and
"callback=DecorateMe.callback" is an error!

And this explains your surprise: certainly there is a previous version
of a DecorateMe class in your module (or interactive session). This
other class has the same name, but it is still a different class, hence
the message.
History
Date User Action Args
2022-04-11 14:56:32adminsetgithub: 46785
2008-04-02 07:37:17amaury.forgeotdarcsetstatus: open -> closed
resolution: not a bug
messages: + msg64839
nosy: + amaury.forgeotdarc
2008-04-02 02:23:49tdimsoncreate