Connecting Tech Pros Worldwide Forums | Help | Site Map

how to dispatch objects depending on their class

Curzio Basso
Guest
 
Posts: n/a
#1: Jul 18 '05

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 thought about
using a dictionary, like:

FUNC = {"<class '__main__.A'>": do_something_with_A,
"<class '__main__.B'>": do_something_with_B}

def func(object):
FUNC[type(object)](object)

But: (1) I am not sure of what the type(object) function return. In this
case A and B are in the __main__ namespace (assuming this is how is
called), but if they are in a module; (2) I am not sure if it is
efficient; and finally (3): probably there is a better way to do it
(this is always a safe assumption).

I hope my problem is clear, and excuse me if I am asking about something
that is common knowledge, but I don't even know what to google for...

thanks, curzio

Peter Otten
Guest
 
Posts: n/a
#2: Jul 18 '05

re: how to dispatch objects depending on their class


Curzio Basso wrote:
[color=blue]
> FUNC = {"<class '__main__.A'>": do_something_with_A,
> "<class '__main__.B'>": do_something_with_B}
>
> def func(object):
> FUNC[type(object)](object)[/color]

You'll have to modify that slightly for it to work. It will be very
efficient, too.

func_dict = {A: do_something_with_A,
B: do_something_with_B}
def func(obj):
func_dict[obj.__class__](obj)

Using classes as keys avoids the ambiguity you fear. The overhead will be
one dictionary lookup and a function call. The main drawback of this simple
approach is that it does not search the inheritance tree but insists on an
exact class match.
[color=blue]
> I hope my problem is clear, and excuse me if I am asking about something
> that is common knowledge, but I don't even know what to google for...[/color]

Maybe

python generic dispatch

Peter


Casey Duncan
Guest
 
Posts: n/a
#3: Jul 18 '05

re: how to dispatch objects depending on their class


It should not be a problem to use the actual class object as keys in the
dict and the functions as values. No need to convert the class to
strings.

So why can't they be static methods of the classes exactly? That would
be simpler, however the dict will work efficiently.

-Casey

On Tue, 10 Aug 2004 16:35:48 +0200
Curzio Basso <curzio.basso@unibas.ch> wrote:
[color=blue]
>
> 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 thought
> about using a dictionary, like:
>
> FUNC = {"<class '__main__.A'>": do_something_with_A,
> "<class '__main__.B'>": do_something_with_B}
>
> def func(object):
> FUNC[type(object)](object)
>
> But: (1) I am not sure of what the type(object) function return. In
> this case A and B are in the __main__ namespace (assuming this is how
> is called), but if they are in a module; (2) I am not sure if it is
> efficient; and finally (3): probably there is a better way to do it
> (this is always a safe assumption).
>
> I hope my problem is clear, and excuse me if I am asking about
> something that is common knowledge, but I don't even know what to
> google for...
>
> thanks, curzio
> --
> http://mail.python.org/mailman/listinfo/python-list
>[/color]

Bruno Desthuilliers
Guest
 
Posts: n/a
#4: Jul 18 '05

re: how to dispatch objects depending on their class


Curzio Basso wrote:[color=blue]
>
> 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.[/color]

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

Guest
 
Posts: n/a
#5: Jul 18 '05

re: how to dispatch objects depending on their class


Visitor Pattern?


Peter Abel
Guest
 
Posts: n/a
#6: Jul 18 '05

re: how to dispatch objects depending on their class


Curzio Basso <curzio.basso@unibas.ch> wrote in message news:<4118dd4a$1@maser.urz.unibas.ch>...[color=blue]
> 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 thought about
> using a dictionary, like:
>
> FUNC = {"<class '__main__.A'>": do_something_with_A,
> "<class '__main__.B'>": do_something_with_B}
>
> def func(object):
> FUNC[type(object)](object)
>
> But: (1) I am not sure of what the type(object) function return. In this
> case A and B are in the __main__ namespace (assuming this is how is
> called), but if they are in a module; (2) I am not sure if it is
> efficient; and finally (3): probably there is a better way to do it
> (this is always a safe assumption).
>
> I hope my problem is clear, and excuse me if I am asking about something
> that is common knowledge, but I don't even know what to google for...
>
> thanks, curzio[/color]


At my opinion you try to do something outside a class which should
be a basic task of OOP. Why not let the instances do their class-specific
things:

class A(object):
def __init__(self):
pass
def do_something(self):
""" Do A-specific things """
pass

class B(object):
def __init__(self):
A.__init__(self)
def do_something(self):
""" Do B-specific things """
pass

a=A()
b=B()
a.do_something()
b.do_something()

Regards
Peter
Curzio Basso
Guest
 
Posts: n/a
#7: Jul 18 '05

re: how to dispatch objects depending on their class


Peter Abel wrote:
[color=blue]
> At my opinion you try to do something outside a class which should
> be a basic task of OOP. Why not let the instances do their class-specific
> things:[/color]

I know that this situation would be typically handled by putting the
logic in the class hierarchy, but as I wrote I don't want this. In fact,
as cmedcoff@hotmail.com pointed out, at the end what I want to implement
is a visitor pattern (maybe I should have simply mentioned this). In C++
then the right implementations are chosen at compile time because of the
function overloading, but in Python I have to find a different way to
implement this.

cheers, curzio
Curzio Basso
Guest
 
Posts: n/a
#8: Jul 18 '05

re: how to dispatch objects depending on their class


Curzio Basso wrote:
[color=blue]
> class B(object):
> def __init__(self):
> A.__init__(self)[/color]

typo. this should be:

class B(A):
def __init__(self):
A.__init__(self)
Curzio Basso
Guest
 
Posts: n/a
#9: Jul 18 '05

re: how to dispatch objects depending on their class


cmedcoff@hotmail.com wrote:
[color=blue]
> Visitor Pattern?[/color]

Actually, I thought what I was describing was a visitor pattern but I am
probably wrong.

Is it correct that in the visitor pattern the dispatching would be
delegated to the classes, like with:

class A(object):
def __init__(self):
pass
def accept(self, visitor)
visitor.do_something_with_A(self)

class B(A):
def __init__(self):
A.__init__(self)
def accept(self, visitor)
visitor.do_something_with_B(self)

class Visitor(object):
def __init__(self):
pass
def do_something_with_A(self, object):
pass
def do_something_with_B(self, object):
pass

Then I would call

a = A()
b = B()
v = Visitor()
a.accept(v)
b.accept(v)

Is this correct?

thanks, curzio
Curzio Basso
Guest
 
Posts: n/a
#10: Jul 18 '05

re: how to dispatch objects depending on their class


Bruno Desthuilliers wrote:
[color=blue]
> I saw something like a multi-methods implementation in Python somewhere,
> this may interest you.
> http://www-106.ibm.com/developerwork.../l-pydisp.html[/color]

looks interesting. thanks for the link, I'll post some comment if I find
it useful for my problem.

cheers, curzio
Curzio Basso
Guest
 
Posts: n/a
#11: Jul 18 '05

re: how to dispatch objects depending on their class


Casey Duncan wrote:
[color=blue]
> It should not be a problem to use the actual class object as keys in the
> dict and the functions as values. No need to convert the class to
> strings.[/color]

Ops, did not think about that. But maybe the string is a more efficient key?
[color=blue]
> So why can't they be static methods of the classes exactly? That would
> be simpler, however the dict will work efficiently.[/color]

Well, as I wrote in the original post and in some reply, I want to keep
the logic of this particular function separated from the class
hierarchy. And as cmedcoff@hotmail.com suggested this could be a Visitor
pattern. I'm looking on that.

cheers, curzio
Curzio Basso
Guest
 
Posts: n/a
#12: Jul 18 '05

re: how to dispatch objects depending on their class


Peter Otten wrote:
[color=blue]
> func_dict = {A: do_something_with_A,
> B: do_something_with_B}
> def func(obj):
> func_dict[obj.__class__](obj)[/color]

ok. it tested and it works. thanks.

now I have only to check if the Visitor Pattern is not more appropriate.

cheers, curzio
Duncan Booth
Guest
 
Posts: n/a
#13: Jul 18 '05

re: how to dispatch objects depending on their class


Curzio Basso <curzio.basso@unibas.ch> wrote in
news:4119d33c$1@maser.urz.unibas.ch:
[color=blue]
> Is it correct that in the visitor pattern the dispatching would be
> delegated to the classes, like with:
>[/color]
<snip>
[color=blue]
> Is this correct?
>[/color]
Roughly yes, but with a dynamic language like Python you can easily extract
the implementation out into a mixin class rather than repeating what is
essentially the same code everywhere. e.g.
[color=blue][color=green][color=darkred]
>>> class VisitorMixin:[/color][/color][/color]
def accept(self, visitor):
getattr(visitor, 'visit_' + self.__class__.__name__)(self)

[color=blue][color=green][color=darkred]
>>> class A(object, VisitorMixin): pass[/color][/color][/color]
[color=blue][color=green][color=darkred]
>>> class B(A): pass[/color][/color][/color]
[color=blue][color=green][color=darkred]
>>> class Visitor:[/color][/color][/color]
def visit_A(self, object):
print "Visiting an A", object
def visit_B(self, object):
print "Visiting a B", object

[color=blue][color=green][color=darkred]
>>> a = A()
>>> b = B()
>>> v = Visitor()
>>> a.accept(v)[/color][/color][/color]
Visiting an A <__main__.A object at 0x00A8BF10>[color=blue][color=green][color=darkred]
>>> b.accept(v)[/color][/color][/color]
Visiting a B <__main__.B object at 0x00A8B530>[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]

In practice the accept function could be more complicated, e.g. visiting
child objects or handling errors more intelligently.
Peter Otten
Guest
 
Posts: n/a
#14: Jul 18 '05

re: how to dispatch objects depending on their class


Curzio Basso wrote:
[color=blue]
> Peter Otten wrote:
>[color=green]
>> func_dict = {A: do_something_with_A,
>> B: do_something_with_B}
>> def func(obj):
>> func_dict[obj.__class__](obj)[/color]
>
> ok. it tested and it works. thanks.
>
> now I have only to check if the Visitor Pattern is not more appropriate.[/color]

I'd say the choice of the dispatch mechanism - dictionary lookup vs Duncan
Booth's name-mangling - is independent of the question whether the visitor
pattern applies. Duncan's setup would then become something like

class VisitorMixin:
def accept(self, visitor):
visitor.dispatch[self.__class__](self)

class Visitor:
def __init__(self):
self.dispatch = {A: self.visit_A, B: self.visit_B}
def visit_A(self, a):
pass
def visit_B(self, b):
pass


Peter

Curzio Basso
Guest
 
Posts: n/a
#15: Jul 18 '05

re: how to dispatch objects depending on their class


Peter Otten wrote:
[color=blue]
> I'd say the choice of the dispatch mechanism - dictionary lookup vs Duncan
> Booth's name-mangling - is independent of the question whether the visitor
> pattern applies. Duncan's setup would then become something like
>
> class VisitorMixin:
> def accept(self, visitor):
> visitor.dispatch[self.__class__](self)
>
> class Visitor:
> def __init__(self):
> self.dispatch = {A: self.visit_A, B: self.visit_B}
> def visit_A(self, a):
> pass
> def visit_B(self, b):
> pass
>[/color]

Yes, the dispatching would be not necessary if no mixin was used. Which
is not desirable, however, because it would add a dependency between the
subclass and the visitor.

thanks again, I got a lot of useful advices.

cheers, curzio
Closed Thread