how to dispatch objects depending on their class

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Tue Aug 10 19:15:55 CEST 2004


Curzio Basso wrote:
> 
> Hi all.
> 
> I have a couple of question regarding the following situation:
> 
> class A(object):
>   def __init__(self):
>     pass
> 
> class B(object):
>   def __init__(self):
>     A.__init__(self)
> 
> def func(object):
>   if isinstance(object, A):
>     do_something_with_A(object)
>   elif isinstance(object, B):
>     do_something_with_B(object)
> 
> Note that in my real problem I cannot move the logic of func to the 
> class A and B because I will have a hierarchy also for func. Then I need 
> a way to dispatch the object to the right function. 

I saw something like a multi-methods implementation in Python somewhere, 
this may interest you.
http://www-106.ibm.com/developerworks/linux/library/l-pydisp.html




More information about the Python-list mailing list