safely rename a method with a decorator
MRAB
google at mrabarnett.plus.com
Sun Mar 22 19:38:31 EDT 2009
Daniel Fetchinson wrote:
> I'd like to implement a decorator that would rename the method which
> it decorates. Since it's a tricky thing in general involving all sorts
> of __magic__ I thought I would ask around first before writing
> something buggy :)
>
> It should work something like this:
>
> class myclass( object ):
> @rename( 'hello' )
> def method( self ):
> print 'ok'
>
> # tests
>
> inst = myclass( )
> inst.method( ) # raise an AttributeError
> inst.hello( ) # prints 'ok'
> myclass.method # raise an AttributeError
> myclass.hello # prints <unbound method myclass.hello>
> assert 'method' in dir( myclass ) is False
> assert 'hello' in dir( myclass ) is True
>
> Any ideas?
>
What is your use case? Why don't you just give the method the right name
in the first place? :-)
More information about the Python-list
mailing list