Short answer
Polymorphism in Python means one function can call the same method on objects of different classes. Duck typing looks for that method or attribute instead of testing the class name. A subclass can override the method, and the call still uses the version on the object you passed.
Last verified against the sources listed below.
Key takeaways
- Duck typing calls the method you need and skips type() and isinstance() checks.
- A derived class can override any method of its base class. Python methods are virtual.
- issubclass(bool, int) is True. issubclass(float, int) is False.
- Call the base method with Base.method(self, ...) or super() when you want to extend it.
- PEP 3119 says a __getitem__ method does not require a Sequence base class.
How to use polymorphism in Python is to call the same method on different objects, and to let each object supply its own version of that method.
The classes tutorial says a class bundles data and functions, and a derived class can override any method of its base. The glossary names the other style duck typing: code uses an attribute because it is there, not because type() said so.
Both styles let one function accept more than one kind of object. Python is the language this tutorial is written for.
This pattern is one reason people file that style under object-oriented work in programming paradigms.
What you need before you start
You need Python 3 and two objects that both provide the method your function will call.
The tutorial this article follows is the Python 3 classes chapter. You do not need a third-party package, and a subclass relationship is optional.
Duck typing works when the objects are unrelated, as long as the method names match. If you do build a hierarchy, the derived class does not need new syntax beyond the base class in parentheses.
Wikipedia’s duck typing page describes the same idea as using the methods and properties an object has, rather than a type declared on it. The Python glossary is the definition to follow in this language. It says duck typing avoids type() and isinstance() tests and typically uses hasattr() or an EAFP style: try the call and handle the failure.
Write a duck-typed function
Duck typing means the function calls a method and does not ask for the class.
The glossary puts it as looking at the interface you use, not the object’s type. The classes tutorial gives the file-shaped version: a function that expects read and readline can take a class that pulls those lines from a string buffer. The class does not have to inherit from a file type.
Here is a complete example under the same idea. talk never checks the class. It only calls speak.
class Dog:
def speak(self):
return "woof"
class Duck:
def speak(self):
return "quack"
def talk(animal):
return animal.speak()
print(talk(Dog()), talk(Duck()))
talk(Dog()) returns woof. talk(Duck()) returns quack. The function is polymorphic because the method that runs depends on the object. PEP 3119 says this style stays valid even with abstract base classes: a class with __getitem__ does not have to inherit from a sequence base before x[y] works.
Follow the numbered steps
Each step is one change. Stop after step 4 if you do not need a class hierarchy.
- Pick the method name callers will share, such as
speakorread. - Define two classes that each implement that method with the behavior you want.
- Write a function that calls the method on its argument and does not call
type()orisinstance(). - Call the function once with an instance of each class and read the return values.
- If one class should start from another, put the base name in parentheses and define the method again to override it.
- If the override should keep the base behavior, call
BaseClassName.method(self, ...)from the derived method.
Step 5 uses the tutorial’s derived-class syntax. Method lookup walks the base classes until it finds a function. The tutorial says that, in C++ terms, Python methods are virtual: a base method that calls another method can land on the override in the derived class.
Override a method in a subclass
A subclass replaces a method by defining the same name, and callers still use one call site.
Instantiation of a derived class is ordinary: call the class. The tutorial says an overriding method may extend the base method instead of discarding it, by calling BaseClassName.methodname(self, arguments). super() is the other tool, and it follows the method resolution order.
That order is not only “left to right” in complex trees. The tutorial says every multiple-inheritance graph has a diamond because all classes inherit from object, so the search is linearized. The Python 2.3 method resolution order essay says the C3 algorithm is what Python 2.3 adopted for new-style classes, and that you can skip the paper if your hierarchy is simple.
A word of caution on checks: issubclass answers a different question. The tutorial states that issubclass(bool, int) is True and issubclass(float, int) is False. Use that when you truly need the class tree. The glossary says duck typing is the path that does not.
Fix the usual failures
When a call blows up, the object is missing the method, the name is misspelled, or a type check rejected a valid object.
| Symptom | Likely cause | What to change |
|---|---|---|
| AttributeError on the call | The object has no method with that name | Add the method, or catch the error if absence is allowed |
| The base result appears | The derived class used a different name | Match the base method name exactly |
| isinstance rejects a working object | The check demands a base class | Delete the check and call the method |
| super() skips a parent you expected | The method order follows C3, not a hand-rolled walk | Read the MRO essay before rearranging bases |
| Data on the instance looks wrong | Another class stored its own attributes | The tutorial says clients can add attributes if names do not clash |
The tutorial also says nothing in Python enforces data hiding. A leading underscore is a convention for a non-public name, not a lock. If two classes must not share a private helper, the name-mangling form __spam rewrites the name with the class, which the tutorial says helps subclasses override methods without breaking internal calls.
Count a draft comment with the word counter if you are writing the docstring that explains the shared method. The counter does not run your code. It only shows how long the text is.
Frequently asked questions
Does the name self mean anything special?
No. The classes tutorial says the first parameter of a method is often called self, and that this is only a convention. Python does not give the name self any special meaning. The instance is passed as the first argument because of how method calls work, not because of the spelling. Calling x.f() is equivalent to MyClass.f(x). Skipping the convention makes the code harder for other Python programmers to read, and a class browser might assume the usual name.
What does issubclass report for bool and float?
The classes tutorial gives two built-in checks. issubclass(bool, int) is True because bool is a subclass of int. issubclass(float, int) is False because float is not a subclass of int. Use isinstance() when you need to test one object against a class. Those checks answer a class relationship. They are a different tool from duck typing, which the glossary says should avoid type() and isinstance() and should call the method instead.
When would you use an abstract base class?
PEP 3119, created in April 2007 for Python 3.0, says abstract base classes do not end duck typing. Defining __getitem__ does not require your class to inherit from a sequence base, and the x[y] syntax does not require x to be an instance of that base. You can still pass any file-like object that has write to sys.stdout. The PEP's reason to opt in is a default implementation or a clearer split between mappings and sequences, not a requirement.
How does multiple inheritance pick a method?
The classes tutorial says a simple mental model is depth-first and left to right, without searching the same class twice. The real order changes to support super(), and it has to handle diamonds because every class inherits from object. The tutorial points to the Python 2.3 method resolution order essay for the full C3 rules. That essay says you can skip the algorithm unless your hierarchy uses multiple inheritance in a non-trivial way.
Can you call a base method from an override?
Yes. The tutorial says an override can extend the base method instead of replacing it. Call BaseClassName.methodname(self, arguments) to hit that version directly. super() is the other path, and the method resolution order is what makes cooperative super() calls work across multiple bases. If you only wanted a different behavior and do not need the base body, skip the call. The derived method is the one a normal lookup finds.
Sources
- 9. Classes · Python Software Foundation, 2026
- Glossary · Python Software Foundation, 2026
- PEP 3119 – Introducing Abstract Base Classes · Python Enhancement Proposals, 2007
- The Python 2.3 Method Resolution Order · Python.org, 2026
- Duck typing · Wikipedia, 2026