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 the default subtitles language 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 see our video player troubleshooting guide to resolve the issue.

Using @singledispatchmethod for Multiple Class Constructors

00:00 Using @singledispatchmethod for Multiple Constructors. The final technique that you’ll learn is known as single-dispatch generic functions. With this technique, you can add multiple constructors to your classes and run them selectively, according to the type of their first argument.

00:20 A single-dispatch generic function consists of multiple functions implementing the same operation for different data types. The underlying dispatch algorithm determines which implementation to run based on the type of a single argument.

00:33 That’s where the term single dispatch comes from. Starting with Python 3.8, you can use this @singledispatch or @singledispatchmethod decorator to turn a function or method, respectively, into a single-dispatch generic function.

00:49 PEP 443 explains that you can find these decorators in the functools module. In a regular function, Python selects the implementation to dispatch according to the type of the function’s first argument. In a method, the target argument is the first one immediately after self.

01:08 To apply the single-dispatch method technique to a given class, you need to define a base method implementation and decorate it with @singledispatchmethod.

01:16 Then you can write alternative implementations and decorate them using the name of the base method plus .register.

01:26 Here’s an example that shows the basic syntax

01:42 in DemoClass. You first define a base method called generic_method() and decorate it with @singledispatchmethod.

01:52 Then you define two alternative implementations of generic_method() and decorate them with @generic_method.register. In this example, you name the alternative implementations using a single underscore (_) as a placeholder name. In real code, you should use descriptive names, providing they’re different from the base method name, generic_method(). When using descriptive names, consider adding a leading _ to mark the alternative methods as non-public and prevent direct calls from your end users. In the first method, you can see how you can use type annotations to define the type of the target argument.

02:29 You can also explicitly pass the type of the target argument to the .register() decorator, as seen in the second method. If you need to define a method to process several types, then you can stack multiple calls to .register(), with the required type for each.

02:43 You’ll see an example of this later on in the course.

02:49 Here’s how the DemoClass works.

02:57 If you call .generic_method() with an integer number as an argument, then Python runs the implementation corresponding to the int type.

03:05 Similarly, when you call the method with a string, Python dispatches the string implementation. Finally, if you call .generic_method() with an unregistered data type, such as a list, then Python runs the base implementation of the method.

03:21 You can also use this technique to overload .__init__(), which will allow you to provide multiple implementations for this method, and therefore your class will have multiple constructors.

03:34 In the next section of the course, you’ll see this technique in action.

Become a Member to join the conversation.