Type Hints and Defaults
Explore how to implement optional type hints and provide default values for arguments in Python class methods. Understand the use of docstrings to document classes and methods effectively, enhancing code readability and maintainability.
We'll cover the following...
We’ve noted a few times now, hints are optional. They don’t do anything at runtime. There are tools; however, that can examine the hints to check for consistency. The mypy tool is widely used to check type hints.
Using default arguments
If we don’t want to make the two arguments required, we can use the same syntax Python functions use to provide default arguments. The keyword argument syntax appends an equals sign after each variable name. If the calling object does not provide this argument, then the default argument is used instead. The variables will still be available to the function, but they will have the values specified in the argument ...