Using the Single-Dispatch Method in the Real World
A Real-World Example of a Single-Dispatch Method. As a more realistic example of using
@singledispatchmethod, say you need to continue adding features to your
Person class. This time, you need to provide a way to compute the approximate age of a person based on their birth date. To add this feature to
Person, you can use a helper class that handles all the information related to the birth date and age.
Go ahead and create a file called
person.py in your working directory. Then add the code seen on-screen to it. Here you import
datetime so that you can later convert any input date to a
@singledispatchmethod to define the overloaded method. This line starts the definition of
BirthInfo as a regular Python class.
These lines define the class initializer as a single-dispatch generic method using
@singledispatchmethod. This is the method’s base implementation, and it raises a
ValueError for unsupported date formats.
These lines register an implementation of
.__init__() that processes
date objects directly.
These lines define the implementation of
.__init__() that processes dates that come as strings with an ISO format.
Here you register implementation that processes dates that come as Unix time in seconds since the epoch. This time, you register two instances of the overloaded method by stacking the
.register decorator with the
Finally, these lines provide a regular method to compute the age of a given person. Note that the implementation of
age() isn’t totally accurate because it doesn’t consider the month and day of the year when calculating the age.
It’s just a bonus feature to enrich this example. Now you can use composition in your
Person class to take advantage of the new
Go ahead and update
Person with the code seen on-screen.
In this update,
Person has a new non-public attribute called
._birth_info, which is an instance of
BirthInfo. This instance is initialized with the input argument
The overloaded initializer of the
BirthInfo class will initialize
._birth_info according to the user’s birth date. Then you define
age() as a property to provide a computed attribute that returns the person’s current approximate age.
The final addition to
Person is the
birth_date() property, which returns the person’s birth date as a
To try out your
BirthInfo classes, open an interactive session and run the code seen on-screen.
You can instantiate
Person using different date formats. The internal instance of
BirthDate automatically converts the input date into a
If you instantiate
Person with an unsupported date format, such as a dictionary, then you get a
ValueError. Note that
BirthDate.__init__() takes care of processing the input birth date for you.
04:18 There’s no need to use explicit alternative constructors to process different types of input. You can just instantiate the class using the standard constructor.
The main limitation of the single-dispatch method technique is that it relies on a single argument, the first argument after
self. If you need to use multiple arguments for dispatching appropriate implementations, then check out some existing third-party libraries, such as
04:52 In the next section of the course, you’ll take a look back at what you’ve learned.
Become a Member to join the conversation.