Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Using the Single-Dispatch Method in the Real World

00:00 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.

00:27 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 date from datetime so that you can later convert any input date to a date object.

00:44 This imports @singledispatchmethod to define the overloaded method. This line starts the definition of BirthInfo as a regular Python class.

00:56 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.

01:11 These lines register an implementation of .__init__() that processes date objects directly.

01:24 These lines define the implementation of .__init__() that processes dates that come as strings with an ISO format.

01:39 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 int and float types.

01:56 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.

02:09 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 BirthInfo class.

02:20 Go ahead and update Person with the code seen on-screen.

02:32 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 birth_date.

02:45 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.

03:03 The final addition to Person is the birth_date() property, which returns the person’s birth date as a date object.

03:17 To try out your Person and BirthInfo classes, open an interactive session and run the code seen on-screen.

03:35 You can instantiate Person using different date formats. The internal instance of BirthDate automatically converts the input date into a date object.

04:04 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.

04:30 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 multipledispatch and multimethod.

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.