polymorphism in python
Jay O'Connor
joconnor at cybermesa.com
Wed Nov 26 18:50:17 EST 2003
Roy Smith wrote:
>Jay O'Connor <joconnor at cybermesa.com> wrote:
>
>
>>Languages like Python [and] Smalltalk [...] are not as good at
>>overloading based on argument types (how do you have two methods
>>that have the same name but one takes a single integer argument
>>and one takes a single string argument?
>>
>>
>
>But how often is it actually useful to do such a thing? I can't
>remember the last time I wanted to do something like that in Python.
>
>
I occasionally run into it when I want to display something where you
can polymorphically ask to display something but how it's displayed
depends on whether it's a number or not. However, it's a failry
contrived example as you can do operator overloading on more than just
strings and integers. Even on objects within a hiearchy. I'm not really
a big fan off it, just that it can be done; and that it's legitamite
polymorphism.
>or you could maybe even do:
>
>def foo (x):
> if type(x) == StringType:
> do string stuff
> else:
> do integer stuff
>
>
Generally, if you are programming to the interface and not to the class
(which is what you are usually doing in OO programming, or should be if
your language allows it) than testing for the actual type of an object
is usually a no-no
More information about the Python-list
mailing list