liveBook · Manning

Function overloading

As explained in Software Design for Python Programmers

Function overloading is providing the same operation through multiple signatures or argument forms with behavior selected according to the call.

Function overloading provides one operation through multiple signatures or argument forms. The forms should perform conceptually similar, identical, or equivalent operations; unrelated behaviors deserve different names. section 7.1.3 section 7.6

Use overloading when:

  • Several calls express the same conceptual operation, such as calculating an area or measuring a line. section 7.1.3
  • Different argument counts or datatypes provide convenient representations of the same result. section 7.1.3
  • A single name is clearer than inventing separate names for strongly related operations. section 7.1.3

Selecting an area calculation

Python does not retain multiple same-named method definitions in one class; the last definition replaces earlier ones. Runtime parameter checking can simulate overloading: Shape.area(2, 3) calculates a rectangle's area, while Shape.area(1.0) calculates a circle's area. The implementation selects the behavior from whether arg2 is present. section 7.1.3

Measuring lines from multiple representations

Line.length accepts endpoint coordinates, a coordinate list, dictionaries, or Point objects, then computes the same distance. Explicit type and argument-count checks support the forms but make the method complex and prone to error. The @multimethod decorator separates each typed implementation and dispatches at runtime; unsupported combinations still produce an error. section 7.1.3 section 7.1.4

How Software Design for Python Programmers covers it

How deep it goes
Explains naming criteria, Python's replacement behavior, runtime checks, and decorator-based dispatch, stopping short of formal language semantics.
Examples
Demonstrates overloaded area calculations, line lengths from points and coordinate containers, the plus operator, and len().
What it assumes
Assumes basic Python classes, methods, arguments, and datatypes while explaining signatures and Python's dynamic overloading limitations.
What you can do after
Build it from scratch using runtime checks or the multimethod decorator to dispatch related calls by argument form and datatype.
How it connects
Connects function overloading with method overriding, type hints, operator overloading, runtime parameter checking, and multimethod dispatch.

Also explained in

Compare how 3 books explain function overloading →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Software Design for Python Programmers. The section links point to the parts of the book it draws on.