Using @singledispatchmethod for Multiple Class Constructors
@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.
That’s where the term single dispatch comes from. Starting with Python 3.8, you can use this
@singledispatchmethod decorator to turn a function or method, respectively, into a single-dispatch generic function.
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
To apply the single-dispatch method technique to a given class, you need to define a base method implementation and decorate it with
Then you can write alternative implementations and decorate them using the name of the base method plus
01:26 Here’s an example that shows the basic syntax
DemoClass. You first define a base method called
generic_method() and decorate it with
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.
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.
Here’s how the
If you call
.generic_method() with an integer number as an argument, then Python runs the implementation corresponding to the
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.
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.