Chapter 19. Data types as objects

This chapter covers

  • Treating types as objects
  • Using types
  • Creating user-defined classes
  • Understanding duck typing

By now, you’ve learned the basic Python types and also how to create your own data types using classes. For many languages, that would be pretty much it, as far as data types are concerned. But Python is dynamically typed, meaning that types of things are determined at runtime, not at compile time. This is one of the reasons Python is so easy to use. It also makes it possible, and sometimes necessary, to compute with the types of objects (and not just the objects themselves).

19.1. Types are objects, too

Fire up a Python session, and try out the following:

>>> type(5) <class 'int'> >>> type(['hello', ...

Get The Quick Python Book, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.