[New-bugs-announce] [issue2533] Invalid TypeError with class method decorator and class method parameter

Thomas Dimson report at bugs.python.org
Wed Apr 2 04:23:59 CEST 2008


New submission from Thomas Dimson <tdimson at gmail.com>:

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.

----------
components: None
messages: 64830
nosy: tdimson
severity: normal
status: open
title: Invalid TypeError with class method decorator and class method parameter
type: behavior
versions: Python 2.4, Python 2.5

__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue2533>
__________________________________


More information about the New-bugs-announce mailing list