Function Overloading and Python

castironpi at gmail.com castironpi at gmail.com
Sun Mar 2 17:43:40 EST 2008


On Feb 25, 11:04 am, castiro... at gmail.com wrote:
> > B1.fun(A(x), A(y), A(z)) == B.fun(A(x), A(y), A(z))
> > but
> > B1.fun(A1(x), A(y), A(z) != B.fun(A1(x), A(y), A(z))
>
> > Is there a data-structure solution or third party module that would
> > mimic this behavior?
>
> class B:
>    xfun= Overloaded()
>    def fun( self, *a ):
>       return self.xfun.dispatch( self, *a )
>    @xfun.make( A, A, A )
>    def q( self, x, y, z ):
>       return 'B AAA'
>    @xfun.make( A1, A, A )
>    def q( self, x, y, z ):
>       return 'B A1AA'
> B.xfun.push( B )

You could also call xfun.methods( self ) in B.__init__.
Overloaded.methods binds the methods specified with xfun.make to the B
instance.  In this case, the effect is,

self.fun= types.MethodType( self.__class__.[x?]fun, self ) -- I forget
by now.

But time is money, and money doesn't grow on trees-- so catch me later
with your own.

(A decorator could also do it too-- and just in the base class!)



More information about the Python-list mailing list