pythonooppolymorphism

Is W3Schools' 'Class Polymorphism' example in Python a valid use of the term 'polymorphism'?


I came across an example on W3Schools where they use the term 'Class Polymorphism' in Python. The example shows three unrelated classes (Car, Boat, and Plane), each with a method called move(). In a loop, they call the move() method on instances of these classes. I don't see this as true polymorphism but more like Python's dynamic method lookup via duck typing.

Is this a valid example of polymorphism or what they call "class polymorphism"(I have never heard of this term before), or is it more of a case of method name coincidence in a dynamically typed language?

The example isn't really showing traditional polymorphism. Usually, polymorphism refers to treating objects of different classes through a shared interface or parent class. But here python is simply looking up whether the move() method exists on each object, which is a result of duck typing rather than polymorphism. Since the classes are unrelated and don't share an interface, there's no real polymorphism happening in my opinion.

Would this be considered proper polymorphism, especially in the context of statically typed languages, or is this simply a feature of Python's dynamic typing?

Reference: https://www.w3schools.com/python/python_polymorphism.asp


Solution

  • In the example from the W3Schools we have a valid example of polymorphism. In particular they show duck typing, which is one form of polymorphism in Python.

    You can read more about it here - Duck Typing in Python: Writing Flexible and Decoupled Code.