liveBook · Manning

Dynamic typing

As explained in Practices of the Python Pro

Dynamic typing is a type-system property in which data types are determined or checked during program execution rather than before execution.

Dynamic typing determines a variable’s data type during program execution rather than requiring type checks before execution. Python is dynamically typed, so the same variable can receive values of different types as a program runs. section 3.5

Runtime behavior in Python

Python attempts method calls during execution. If an object lacks the requested method, the call raises AttributeError rather than failing during compilation. This supports duck typing: objects can provide compatible behavior without sharing the same class. section 3.5

Dynamic typing affects everyday Python design in several ways:

  • Inspect objects with type(), isinstance(), and issubclass() when debugging makes an object’s type unclear. section 8.3.1
  • Use type hints as guidance for developers and automated tools; Python does not enforce them during execution. section 8.3.1
  • Prefer behavior and shared messages over rigid type-dependent designs when creating loosely coupled code. section 10.1.3
  • Recognize that dynamic typing can remove the need for some creational design patterns developed for statically typed languages. section 11.2.1
>>> type(42)
<class 'int'>
>>> type({'dessert': 'cookie', 'flavor': 'chocolate chip'})
<class 'dict'>

Inspecting values such as integers and dictionaries makes runtime type determination concrete. section 8.3.1

How Practices of the Python Pro covers it

How deep it goes
Explains runtime behavior, duck typing, inspection tools, and type hints, but stops short of detailed type-system internals.
Examples
Demonstrates runtime method calls, AttributeError, and type(), isinstance(), and issubclass() inspections.
What it assumes
Assumes basic variables, objects, methods, and classes while introducing runtime types, duck typing, and type inspection.
What you can do after
Diagnose/debug with it by inspecting runtime objects and design Python interactions around supported behavior rather than rigid class hierarchies.
How it connects
Connects dynamic typing with duck typing, polymorphism, type hinting, loose coupling, and design patterns.

Also explained in

Compare how 6 books explain dynamic typing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practices of the Python Pro. The section links point to the parts of the book it draws on.