Perhaps I am just dumb

Quinn Dunkan quinn at hurl.ugcs.caltech.edu
Sun Feb 10 16:45:02 EST 2002


On 10 Feb 2002 00:22:57 -0800, wooks <wookiz at hotmail.com> wrote:
>So they are just syntatical sugar. That description would immediately
>explain their raison d'etre. Shame it wasn't used. Anyway now you are
>saying that my understanding of polymorphism isn't what it was so it
>looks like I need to go and reread. I think I ought to understand it
>as it is a fundamental OO concept which I would like to be able to
>recognise when I come across it.

Hmm, what I was trying to say is that you don't *need* an understanding of
polymorphism.  Your understanding may be perfectly good, but if you spend a lot
of time thinking about it, you may be thinking too hard.

Polymorphism has traditionally been a big deal for static languages, because
their compile time typechecking machinery needs to be powerful enough to
generate polymorphic signatures.  Dynamic languages don't have compile time
typechecking machinery, so it's not an issue.

My understanding of polymorphism is (and I've never taken a CS class so who
knows) that it's when a function can operate on more than one type.  Very
simple, and orthogonal to OO.  For example, python's '+' works on numbers
and strings, so it's polymorphic.  If you have 'sum(a)' which uses '+' to add
up the elements of a list, it will also work to concatenate a list of strings.

For that matter, '+' works on both floats and ints in C, so it's polymorphic
there too.  But if you wanted to *write* a function like '+' in C, it would
want you to declare the types of the arguments, at which point you'd be stuck
between saying "add() takes two ints" and "add() takes two floats" (in the C
case, ints have built in magic that let them automatically turn into floats,
but that's just for numbers (and I assume to make C's lack of polymorphism more
bearable) and not all static languages do automatic conversion like that (e.g.
in haskell "let {x=1::Int; y=2.0} in x+y" is an error)) so you can see why
static languages make a ruckus about supporting (or not) polymorphism, while
dynamic programmers might not even know what it is, but use it all the time
anyway.



More information about the Python-list mailing list