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

Duncan Booth duncan.booth at invalid.invalid
Thu Jun 8 09:15:51 EDT 2006


Ilias Lazaridis wrote:

> 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: 

This is a Python list. Would you care to explain what alias_method does?

> 
> (example from an simple evolution support for a ruby orm)
> 
> #----------------------------------------------------------------------
> -------- # use "alias_method" to enlink the code
> #----------------------------------------------------------------------
> -------- 
> 
>      class SqliteAdapter
>          alias_method :old_create_table,  :create_table
>          def create_table(*args)
>              table_evolve(*args)
>              result = old_create_table(*args)
>              return result
>          end
>      end
> 

This looks like alias_method does nothing much more than an assignment. If 
you want to override a method in a base class then you can do it something 
like:

class SqliteAdapter(BaseClass):
    old_create_table = BaseClass.create_table
    def create_table(self, *args)
        self.table_evolve(*args)
        result = self.old_create_table(*args)
        return result

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

If that isn't what you are trying to achieve you'll have to explain more.



More information about the Python-list mailing list