Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
  #1  
Old July 14th, 2002, 02:13 PM
danaoredson danaoredson is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: Minnesota
Posts: 0 danaoredson User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
polymorphic function selection

want to be able to create some functions that take objects as arguments, and
decide which function is invoked based on the type of the object passed, much
like you'd do in c++..

But, since functions do not declare the types for parameters in python, how
can this be done?

sample c++ syntax that I'd like to be able to handle:


void foo(int a)
{
}

void foo(SomeClass& s)
{
}

void foo(SomeOtherClass& o)
{
}

now, I'd like to use these functions like this:

int a;
SomeClass s;
SomeOtherClass o;

foo(a);
foo(s);
foo(o):

can this be done in python? If not, it seems like a horrible oversight.

Reply With Quote
  #2  
Old July 21st, 2002, 10:12 AM
OB_redemption's Avatar
OB_redemption OB_redemption is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2002
Posts: 32 OB_redemption User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
nope you can't do that in Python... you can, however, do what you want with some simple logic using the type() function to determine the type of the arguments and then deciding what exactly to do...

it's not a horrible oversight i think... i can't explain why but i believe it has to do with Python's dynamic typing feature... a guru could explain better

Reply With Quote
  #3  
Old July 21st, 2002, 10:46 AM
danaoredson danaoredson is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: Minnesota
Posts: 0 danaoredson User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks for your reply. I did some reading, and I agree with your assessment. It's just cumbersome to replace the above code with an if-elif-else. It would be better with a switch statement, but of course, Python doesn't have that either...

Besides those 2 failings, it's a pretty cool language!

Reply With Quote
  #4  
Old July 24th, 2002, 02:44 AM
OB_redemption's Avatar
OB_redemption OB_redemption is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2002
Posts: 32 OB_redemption User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
not having a switch statement isn't a failing... Python's syntax is so clean that you can write a simple if-elif-else statement in place of a switch and still have legible code...

anyway, yes your right Python's a pretty neat language and i'm really 'into' it right now

Reply With Quote
  #5  
Old July 25th, 2002, 07:17 AM
AntonMuhin AntonMuhin is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 0 AntonMuhin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi.

You may address your question to Python lists and I think it'll help you.

Overloading is a technique depricated by a lot of theoretitions and I think it's a god idea not to support overloading directly.

Nonetheless, one can easily implement such functionality. I don't think that 'swict' of 'if-elif-else' is a good way to do it, but dictionaries could be of great help. Let's consider an example:

class A: pass
class B: pass

def overloaded(obj)
def fooA(obj): return "In fooA"
def fooB(obj): return "In fooB"

dispatcher = {A: fooA, B: fooB}
return (dispatcher[obj.__class__])(obj)

Then
print overloaded(A())

should print "In fooA"

print overlaoded(B())

should print "In fooB"

Hope it helps,
Anton.

Reply With Quote
  #6  
Old July 26th, 2002, 11:47 PM
OB_redemption's Avatar
OB_redemption OB_redemption is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2002
Posts: 32 OB_redemption User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
excellent idea Anton!

Reply With Quote
  #7  
Old July 27th, 2002, 08:44 AM
moramis moramis is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 1 moramis User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
polymorphic function selection

Dispatch tables are very handy things when your keys are strings or other literals, but not such a good idea for classes. This is mainly because you lose all potential for catching inherited classes. The proper solution requires isinstance, which in turn requires an if-else ladder, though putting this in the called function isn't as ugly as described. An untested example:
Code:
class A: pass
class B: pass
class C(A): pass

def foo(obj):
    if isinstance(obj, A):
        return fooA(obj)
    elif isinstance(obj, B):
        return fooB(obj)
    else:
        raise TypeError

def fooA(obj): return "In fooA"
def fooB(obj): return "In fooB"


print foo(A())

should print "In fooA"

print foo(B())

should print "In fooB"

print foo(C())

should print "In fooA"


If you are really set on a dispatch table, you could modify the above with:

Code:
dispatch_table = {
    A: fooA,
    B: fooB,
}

def foo(obj):
    for x in dispatch_table.keys():
        if isinstance(obj, x):
            return dispatch_table[x](obj)
    raise TypeError


You should get the same results.

This is especially important since you don't know who will subclass the expected classes in the future and call your functions. You will really lose polymorphic potential here.

Reply With Quote
  #8  
Old July 29th, 2002, 04:13 AM
AntonMuhin AntonMuhin is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 0 AntonMuhin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi all,

Thanks to OB_redemption for kind words.

And special thanks to moramis --- I really forgot about inheritance.

Just a little note: if one want to support inheritance, lists or tuples could be better solution. Lists are better, if you want to override functionaltiy for base class, for lookup order for dictionaries is undefined (at least one shouldn't rely on it) and with lists one can simple added handlers at the beginning
Anyway general overloading could be not so easy to implement.

And the last: in a lot of cases overloading can be implemented with class methods:

class A:
def doIt(): print "In A"
class B:
def doIt(): print "In B"

def foo(obj): obj.doIt() # maybe add a test for presence of the method

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > polymorphic function selection


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway