GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — Polymorphism in Python - GeeksforGeeks Polymorphism in PythonLast Updated : 19 Sep, 2026 Polymorphism means "many forms" and allows the same method, function or operator to behave differently depending on the object or data it works with. This flexibility helps create more reusable, maintainable and scalable code. Real-Life Example:
Types of PolymorphismPolymorphism refers to the ability of the same method or operation to behave differently based on object or context. It mainly includes compile-time and runtime polymorphism. ![]() Let's discuss each type in detail. 1. Compile-time PolymorphismCompile-time polymorphism allows multiple methods with the same name but different parameters, with the appropriate method selected before execution. While languages like Java and C++ support this through method overloading, Python achieves similar behavior using default arguments, *args, and **kwargs because method resolution happens at runtime. Example: This code demonstrates overloading-like behavior using default and variable-length arguments. While Python does not support true method overloading, the same method can handle different numbers of arguments.
Output 1 4 6 24 Explanation:
2. Runtime Polymorphism (Overriding)Runtime polymorphism means that the behavior of a method is decided while program is running, based on the object calling it. This happens through Method Overriding a child class provides its own version of a method already defined in the parent class. Example: This code shows runtime polymorphism using method overriding. The sound() method is defined in base class Animal and overridden in Dog and Cat. At runtime, correct method is called based on object's class.
Output Bark Meow Some generic sound Explanation: Here, sound method behaves differently depending on whether object is a Dog, Cat or Animal and this decision happens at runtime. This dynamic nature makes Python particularly powerful for runtime polymorphism. Built-in FunctionsPython's built-in functions such as len() and max() are polymorphic because they work with different data types and return results based on type of object passed. This showcases it's dynamic nature, where same function name adapts its behavior depending on input. Example: This code demonstrates polymorphism in built-in functions handling strings, lists, numbers and characters differently while using same function name.
Output 5 3 3 z Explanation: len() works with different iterable types such as strings and lists and max() returns the largest value for both numbers and strings. Polymorphism in FunctionsPolymorphism allows functions to work with different object types as long as they support the required behavior. Using duck typing, it focuses on whether an object has the required methods rather than its type, enabling flexible and reusable code. Example: This code demonstrates polymorphism using duck typing as perform_task() function works with different object types (Pen and Eraser), as long as they have a .use() method showing flexible and reusable function design.
Output Writing Erasing Polymorphism in OperatorsSame operator (+) can perform different tasks depending on operand types. This is known as operator overloading. This flexibility is a key aspect of polymorphism. Example: This code shows operator polymorphism as + operator behaves differently based on data types adding integers, concatenating strings and merging lists all using same operator.
Output 15 Hello World! [1, 2, 3, 4] Comment Explore |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |