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

Ilias Lazaridis ilias at lazaridis.com
Thu Jun 8 13:59:31 EDT 2006


Maric Michaud wrote:
> Le Jeudi 08 Juin 2006 15:15, Duncan Booth a écrit :
>> but the more usual way is just to call the original method directly in the
>> base class.
>>
>> class SqliteAdapter(BaseClass):
>>     def create_table(self, *args)
>>         self.table_evolve(*args)
>>         result = BaseClass.create_table(self, *args)
>>         return result
>>
> 
> Yeah, this the right way to reuse ancestor's implementation of a method.
> 
>> If that isn't what you are trying to achieve you'll have to explain more.
> I'm not a ruby programmer, but I understood it like this : the prupose is to 
> modify the behavior of an existing third-party class, in all application 
> (even in existing third party modules), without any code modifications 
> (traditional patch) in those modules.

yes, you've understood right.

> Your proposal is not as good here, assuming BaseClass is defined in module 
> toto, you can still do toto.BaseClass = SqliteAdapter, but you must ensure 
> that this code is imported before any other where classes inherit from 
> BaseClass. The one I porpose in my other post is robust, several packages can 
> even patch the same method with no side effects.

Your suggestion is most possibly the relevant construct.

I'll post a note after changing the implementation.

Thank's a lot!

.

-- 
http://lazaridis.com



More information about the Python-list mailing list