Self-referencing decorator function parameters

Thomas Dimson tdimson at gmail.com
Wed Apr 2 08:30:06 EDT 2008


Hello,

Originally I posted this as a bug but it was shot down pretty quickly.
I am still mildly curious about this as I'm missing a bit of
understanding of Python here. Why is it that the following code
snippet:

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

    return inner

class DecorateMe:
    @decorator( call=DecorateMe.callMe )
    def youBet( self ):
        pass

    def callMe( self ):
        print "Hello!"

DecorateMe().youBet()

Will not compile, giving:
Traceback (most recent call last):
  File "badpython.py", line 10, in <module>
    class DecorateMe:
  File "badpython.py", line 11, in DecorateMe
    @decorator( call=DecorateMe.callMe )
NameError: name 'DecorateMe' is not defined

Where if you change the "call=DecorateMe.callMe" to "call=lambda x:
DecorateMe.callMe(x)" everything goes along its merry way. Nesting the
call in a lambda seems to allow it to recognize the class definition.
Any ideas as to what is going on here (other than ugly code)?

Thank you,
Thomas Dimson



More information about the Python-list mailing list