CONSTRUCT - Python's way of Ruby's "alias_method"

Maric Michaud maric at
Thu Jun 8 15:01:59 CEST 2006

Le Jeudi 08 Juin 2006 14:28, Ilias Lazaridis a écrit :
> Another possibility is to enlink (hook?) the functionality into an
> existent function
> Is there any way (beside a patch) to alter the behaviour to an existing
> function. Is ther a python construct similar to the "alias_method" of Ruby:
No, there is no special construct to do this, but we do things very similar 
every day in Zope, it's called "monkey patch" :
from toto import service

def my_impl(self, *args) :
	old_result = self._old_method(*args)
	# ...
	return new_result

if not hasattr(service, '_old_method') :
	service._old_method = service.method
	service.method = my_impl

once this file is imported, all future calls to "method" of service instances 
will use my_impl.


Maric Michaud

Aristote -
3 place des tapis
69004 Lyon
Tel: +33 426 880 097

More information about the Python-list mailing list