Self-referencing decorator function parameters
castironpi at gmail.com
castironpi at gmail.com
Wed Apr 2 11:32:57 EDT 2008
On Apr 2, 7:30 am, Thomas Dimson <tdim... at gmail.com> wrote:
> 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)?
def decorator( call ):
def inner(func):
def application( *args, **kwargs ):
call(*args,**kwargs)
func(*args,**kwargs)
return application
return inner
class DecorateMe:
def callMe( self ):
print( "Hello!" )
@decorator( call=callMe )
def youBet( self ):
pass
DecorateMe().youBet()
More information about the Python-list
mailing list