definition of 'polymorphism' and python

bruno at modulix onurb at xiludom.gro
Thu Dec 15 20:04:36 CET 2005


Gabriel Zachmann wrote:
> I understand the Wikipedia article on Polymorphism
> ( http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29 )
> that it doesn't make sense to talk about polymorphism in a fully
> dynamically typed language 

"a single polymorphic operator can act in expressions of various types"

>>> list1 = [1,2,3]
>>> list2 = [4,5,6]
>>> list1 + list2
[1, 2, 3, 4, 5, 6]
>>> int1 = 1
>>> int2 = 2
>>> int1 + int2
3
>>> str1 = "aaa"
>>> strb = "bbb"
>>> str1 + strb
'aaabbb'
>>>

Ok, so we have polymorphic operators (if that wasn't obvious)

"A function that can evaluate to and be applied to values of different
types is known as a polymorphic function."

>>> id(1)
5279832
>>> id('aaa')
46912620984672
>>> id(list1)
46912620841680
>>> id(object())
46912496358832
>>> id(object.__class__)
46912499398624
>>> id(id)
46912496264616
>>>

Ok, so we have polymorphic functions too (if that wasn't obvious...)

"If all code is written without mention of any specific type and thus
can be used transparently with any number of new types, it is called
parametric polymorphism"

Well, seems that's what we (mostly) have

-- does the Python community agree?

I'm not the Python communauty, but I think you already guessed my answer !-)


-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list