Unlock Polymorphism with Built-in Functions in Python

Discover the true potential of Python’s built-ins and learn how they facilitate polymorphism

Max N
3 min readMar 31, 2024

Python’s rich ecosystem includes numerous built-in functions spanning diverse categories, such as mathematical computations, file management, and memory manipulations. Often overlooked, these tools offer incredible opportunities for applying polymorphism, leading to clearer, more efficient code.

Built-in functions exhibit polymorphic qualities due to their inherent flexibility regarding input parameters. Regardless of data types provided, built-ins attempt processing inputs until encountering insurmountable obstacles. This section explores five prominent examples highlighting the symbiotic relationship between built-ins and polymorphism.

sum() Function

Summation serves as a fundamental mathematical operation. Fortunately, Python incorporates a built-in sum() function supporting various data types, including numbers, lists, generators, and even iterators.

values = range(10)
print(sum(values)) # Output: 45

squares = (x**2 for x in values)
print(sum(squares)) # Output: 285

Underneath the hood, sum() delegates addition logic to the _operator, thereby harnessing the…

--

--

Max N

A writer that writes about JavaScript and Python to beginners. If you find my articles helpful, feel free to follow.