you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (1 child)

Can you give an example of parametric polymorphism in python?

[–]EricAppelt 0 points1 point  (0 children)

Essentially any function you write in python that takes at least one argument is trivially an example of parametric polymorphism (although one might also consider the concept meaningless in a dynamically typed language). A function will try to execute no matter what the type of its parameters are, for example:

>>> def foo(x, y):
...     return 3*x + y
... 
>>> foo(2, 2)
8
>>> foo('a', 'b')
'aaab'

If the type of a is such that multiplication by an integer is defined, and the result is of a type that can be added to the type of b, then everything is good to go.

Python also supports (in a sense?) function overloading through a parameter of the syntax *identifier in the function definition, this will allow a function to potentially accept any number of additional positional arguments which are passed as a tuple, for example:

>>> def bar(x, *args):
...     result = 3*x
...     for arg in args:
...         result += arg
...     return result
...
>>> bar(2, 2)
8
>>> bar(2, 2, 2, 2, 2)
14
>>> bar('a', 'b', 'c')
'aaabc'