473,507 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to dispatch objects depending on their class


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
Jul 18 '05 #1
14 1449
Curzio Basso wrote:
FUNC = {"<class '__main__.A'>": do_something_with_A,
"<class '__main__.B'>": do_something_with_B}

def func(object):
FUNC[type(object)](object)
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.
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...


Maybe

python generic dispatch

Peter
Jul 18 '05 #2
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 <cu**********@unibas.ch> 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 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


Jul 18 '05 #3
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/developerwork.../l-pydisp.html

Jul 18 '05 #4
Visitor Pattern?
Jul 18 '05 #5
Curzio Basso <cu**********@unibas.ch> wrote in message news:<41********@maser.urz.unibas.ch>...
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

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
Jul 18 '05 #6
Peter Abel wrote:
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:


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 cm******@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
Jul 18 '05 #7
Curzio Basso wrote:
class B(object):
def __init__(self):
A.__init__(self)


typo. this should be:

class B(A):
def __init__(self):
A.__init__(self)
Jul 18 '05 #8
cm******@hotmail.com wrote:
Visitor Pattern?


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
Jul 18 '05 #9
Bruno Desthuilliers wrote:
I saw something like a multi-methods implementation in Python somewhere,
this may interest you.
http://www-106.ibm.com/developerwork.../l-pydisp.html


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

cheers, curzio
Jul 18 '05 #10
Casey Duncan wrote:
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.
Ops, did not think about that. But maybe the string is a more efficient key?
So why can't they be static methods of the classes exactly? That would
be simpler, however the dict will work efficiently.


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 cm******@hotmail.com suggested this could be a Visitor
pattern. I'm looking on that.

cheers, curzio
Jul 18 '05 #11
Peter Otten wrote:
func_dict = {A: do_something_with_A,
B: do_something_with_B}
def func(obj):
func_dict[obj.__class__](obj)


ok. it tested and it works. thanks.

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

cheers, curzio
Jul 18 '05 #12
Curzio Basso <cu**********@unibas.ch> wrote in
news:41********@maser.urz.unibas.ch:
Is it correct that in the visitor pattern the dispatching would be
delegated to the classes, like with:
<snip>
Is this correct?

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.
class VisitorMixin: def accept(self, visitor):
getattr(visitor, 'visit_' + self.__class__.__name__)(self)

class A(object, VisitorMixin): pass class B(A): pass class Visitor: def visit_A(self, object):
print "Visiting an A", object
def visit_B(self, object):
print "Visiting a B", object

a = A()
b = B()
v = Visitor()
a.accept(v) Visiting an A <__main__.A object at 0x00A8BF10> b.accept(v) Visiting a B <__main__.B object at 0x00A8B530>


In practice the accept function could be more complicated, e.g. visiting
child objects or handling errors more intelligently.
Jul 18 '05 #13
Curzio Basso wrote:
Peter Otten wrote:
func_dict = {A: do_something_with_A,
B: do_something_with_B}
def func(obj):
func_dict[obj.__class__](obj)


ok. it tested and it works. thanks.

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


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

Jul 18 '05 #14
Peter Otten wrote:
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


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
Jul 18 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2284
by: Garma | last post by:
According to what I have learnt so far, instantiating global objects should be the last resort. Is there any reasons why so? Sometimes some objects or their pointers have to be shared among...
4
2386
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a...
1
2298
by: Thomas Matthews | last post by:
Hi, I'm looking for an efficient method to deep copy containers of fields. A Field is a parent class with children such as Integer_Field, String_Field, and Date_Field, etc. The algorithm /...
3
5033
by: Dan Vogel | last post by:
I'd like to find an elegant solution to the problem of calling a certain function based on the types of two parameters. In my case, the functions compute the distance between different types of...
3
6899
by: tyler.schlosser | last post by:
Hi there, I am trying to launch a program called AmiBroker using the command: AB = win32com.client.Dispatch("Broker.Application") However, I have a dual-core CPU and would like to launch two...
27
2527
by: SasQ | last post by:
Hello. I wonder if literal constants are objects, or they're only "naked" values not contained in any object? I have read that literal constants may not to be allocated by the compiler. If the...
3
3762
by: Tigera | last post by:
Greetings, I too have succumbed to the perhaps foolish urge to write a video game, and I have been struggling with the implementation of multiple dispatch. I read through "More Effective C++"...
14
5980
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
19
10727
by: Daniel Pitts | last post by:
I have std::vector<Base *bases; I'd like to do something like: std::for_each(bases.begin(), bases.end(), operator delete); Is it possible without writing an adapter? Is there a better way? Is...
0
7221
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7109
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5619
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5039
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3190
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1537
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.