Polymorphism in Python means that different objects can respond to the same method call in their own way, so one piece of code works with many types. You implement it with method overriding in subclasses, with duck typing (any object that has the right methods works), with typing.Protocol for structural typing, and with operator overloading through special methods such as __add__.
Python does not have compile-time method overloading like Java. Instead, the call is resolved at runtime on the actual object, which is why polymorphism is so natural in the language.
typing.ProtocolA subclass can replace a method of its parent. Code that holds a reference to the parent type calls the right version automatically.
describe is written once in Shape, yet it calls whichever area belongs to the real object. Use super().method() inside an override when you want to extend the parent behaviour instead of replacing it.
Raising NotImplementedError only fails when the method is called. An abstract base class from abc fails earlier, when you try to create an incomplete subclass.
Use an abstract base class when subclasses share real code or when you want a clear, explicit contract.
Python follows the rule "if it walks like a duck and quacks like a duck, it is a duck". A function only cares that the object has the attributes it uses.
Dog and Robot are unrelated classes, but both work because both have speak(). Built-in functions such as len() and sum() rely on the same idea.
typing.ProtocolDuck typing is flexible but invisible to type checkers. A Protocol describes the required methods so tools such as mypy and pyright can check the call, without forcing inheritance.
Any class with a matching speak method satisfies Speaker. Add @runtime_checkable if you also need isinstance checks, which only verify that the methods exist.
Special (dunder) methods let your classes plug into Python syntax. The same operator then behaves differently per type.
Return NotImplemented from a special method when you do not support the other operand, so Python can try the reflected method of the other object.
Python keeps only the last definition of a function name, so two def area(...) with different parameters do not coexist. Choose one of these patterns:
| Need | Use |
|---|---|
| Optional arguments | Default values, *args, keyword arguments |
| Behaviour depends on the type of the first argument | functools.singledispatch |
| Behaviour depends on the shape of a value | match statement (Python 3.10+) |
The common forms are method overriding (subtype polymorphism), duck typing, operator overloading and generic dispatch with singledispatch. Parametric polymorphism is expressed with generics in type hints.
Not in the Java or C++ sense. A later def with the same name replaces the earlier one. Use default arguments, *args, singledispatch or match instead.
Neither is always better. Duck typing and protocols keep code loosely coupled, while inheritance is useful when subclasses share implementation.
Prefer just calling it and handling AttributeError where it matters (EAFP style). For static checking, annotate with a Protocol.
Polymorphism lets one interface serve many implementations. In Python you get it through overridden methods, abstract base classes, duck typing, protocols, operator overloading and singledispatch. Pick the lightest tool that makes the contract clear, and let type hints document it.