Type Hints
1. Type Hints
Welcome back! Let's explore type hints, and how they can help supercharge the classes you build.2. Code without type hints
All things considered, the classes we've built so far have been pretty simple; it's been easy to understand the data attributes are storing, and how objects are created and used. But if we didn't write this code, we might feel a bit differently. Should student_id be an integer, or a string? Which type should tuition_balance take? Which type does walker take, and what can be done with that object? Type hinting can help with this!3. Type hints
Type hints are an optional tool that allow information, or "hints", about the type of an object to be added to code. Type hints help make code easier to read and troubleshoot. Adding type hints to your code is one of the best ways to illustrate enterprise-grade Python skills. Type hints aren't enforced by the Python interpreter, meaning the actual type of an object can in fact be different than what was originally hinted. To create type hints, we'll use built-in type keywords, the typing library, and custom classes. First up, built-in type keywords.4. Type hinting with built-in type keywords
To hint that name should be of type string, student_id an integer, and tuition_balance a float, we've added type hints to each declaration using the syntax variable, colon, and the type of the resulting object. We can also type hint parameters in a function or method signature using this same syntax, which we've done for the semester parameter in the get_schedule function. To hint the return type of a function or method, we'll use an arrow and the return type after the last parenthesis but before the colon. If a method or function returns None, we can hint this using the None keyword.5. typing Library
The typing library is the most popular tool used to unlock the full functionality of type hinting. With classes such as List, Dict, and Tuple, the typing library allows both top-level objects to be hinted, as well as the elements of those objects. Here, the student_names variable stores a list of strings. To hint this, we've used the List class from the typing library and the str keyword with the syntax List-str. Using the Dict class, we can hint that the student_gpas dictionary is made up of string keys and float values using the syntax Dict-str-float. The typing library includes a number of other classes used for type hinting, including Any, Set, Iterator, and Callable.6. Type hinting with custom classes
Besides the built-in type keywords and classes from the typing library, we can use almost any class to add type hints to our code. Here, we've type hinted walker using the Student class. This helps developers identify the object stored using walker, and what can be done with it. The get_course method returns a Course class, hinted both in the method definition, as well as when defining the data_science variable.7. Checking object types
Last step; let's validate our type hints! Using the print and type keywords, we can see the type of the object walker is indeed Student. We can also confirm data_science takes type Course in a similar way. When type hinting, the type keyword is your best friend!8. Let's practice!
Fantastic! Now, it's your turn to practice type hinting!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.