Type Hints

Learn about the classes and types in Python.

We have a design in hand and are ready to turn that design into a working program. Of course, it doesn’t usually happen this way. We’ll see examples and hints for good software design throughout the course, but our focus is object-oriented programming. So, let’s have a look at the Python syntax that allows us to create object-oriented software. After completing this chapter, we will understand the following:

  • Python’s type hints
  • Creating classes and instantiating objects in Python
  • Organizing classes into packages and modules
  • How to suggest that people don’t clobber an object’s data, invalidating the internal state
  • Working with third-party packages available from the Python Package Index, PyPI

Introducing type hints

Before we can look closely at creating classes, we need to talk a little bit about what a class is and how we’re sure we’re using it correctly. The central idea here is that everything in Python is an object.

When we write literal values like "Hello, world!" or 42, we’re actually creating instances of built-in classes. We can fire up interactive Python and use the built-in type() function on the class that defines the properties of these objects:

Get hands-on with 1400+ tech skills courses.