June 2019
Beginner to intermediate
770 pages
19h 24m
English
When we implement the __getitem__(), __setitem__() and __delitem__() methods, we must work with two kinds of argument values: int and slice. This variant behavior requires two different type hints. The hints are provided using the @overload decorator.
When we overload the various sequence methods, we must handle the slice situation appropriately within the body of the method. This requires using the isinstance() function to discern whether a slice object or a simple int has been provided as an argument value.
Here is a __setitem__() method that works with slices:
@overloaddef __setitem__(self, index: int, value: float) -> None: ...@overloaddef __setitem__(self, index: slice, value: ...
Read now
Unlock full access