If you're using Python 3.4 or newer, you have access to
functools.singledispatch (thanks to PEP 443), which lets you overload functions (preferably unary) much like you would in a language like Java.
If you define your various polyhedra as classes, you can dispatch on their types, like so:
from functools import singledispatch
def __init__(self, x):
self.x = x
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def __init__(self, *, r, h):
self.r = r
self.h = h
# Mark this function as single-dispatchable.
pass # Or throw an exception, or something like that.
return cube.x ** 3
return cuboid.x * cuboid.y * cuboid.z
return math.pi * (cylinder.r) ** 2 * cylinder.h
# Register handlers for the types for which you can compute volumes.
Now, you can do this:
>>> cube = Cube(4)
>>> cuboid = Cuboid(3, 5, 7)
>>> cylinder = Cylinder(r=2, h=4)
If you're using a version older than 3.4, you don't really have any good options for this sort of transparent single dispatch (though you could always just backport
functools.singledispatch if you're on an earlier 3.x - it's written in pure Python and I don't think it relies on any new 3.4 features). You could, instead, have a function
volume(cube=None, cuboid=None, cylinder=None), or
volume(polyhedron) and dispatch on
type(polyhedron) inside the function, or do some magic with
All that said, I somehow doubt that your book wants you to use
functools.singledispatch - I'd hazard a guess that somebody just copied the problems out of a C++ or Java book without really thinking about it.