Loading video player…

Using Type Hints for Multiple Return Types in Python (Overview)

In Python, type hinting is an optional yet useful feature to make your code easier to read, reason about, and debug. With type hints, you let other developers know the expected data types for variables, function arguments, and return values. As you write code for applications that require greater flexibility, you may need to specify multiple return types to make your code more robust and adaptable to different situations.

You’ll encounter different use cases where you may want to annotate multiple return types within a single function in Python. In other words, the data returned can vary in type. In this video course, you’ll walk through examples of how to specify multiple return types for a function that parses a string from an email address to grab the domain name.

In addition, you’ll see examples of how to specify type hints for callback functions or functions that take another function as input. With these examples, you’ll be ready to express type hints in functional programming.

Download

Course Slides (.pdf)

7.0 MB
Download

Sample Code (.zip)

5.1 KB

00:00 Using Type Hints for Multiple Return Types in Python. In Python, type hinting is an optional but useful feature to make your code easier to read, reason about, and debug. With type hints, you let other developers know the expected data types for variables, function arguments, and return values.

00:18 As you write code for applications that require greater flexibility, you may need to specify multiple return types to make your code more robust and adaptable to different situations.

00:28 You’ll encounter use cases where you may want to annotate multiple return types within a single function in Python. In other words, the data return can vary in type, and this means you’ll need to know how to specify multiple return types for a function.

00:43 In this course, you’ll see how to use type hints when returning multiple data types, see how to declare a function to take a callback, annotate return values of factory functions and generators, improve readability with type hints, and leverage type checking tools.

01:01 To get the most out of this course, you should know the basics of what type hints in Python are and how to use them and Real Python has you covered with this course.

01:11 Any code that you see running in the REPL will be using the Bpython interpreter. This is a replacement Python interpreter that offers a number of enhancements, including code highlighting and suggestions.

01:21 But any code you see running on screen will work in the Python REPL, which is typically accessed by typing python or python3 at the terminal or command-line prompt.

01:31 So now you know what’s going to be covered. Let’s get started.

Become a Member to join the conversation.