Dynamically typed code

As mentioned earlier, bodies of functions that don’t have any explicittypes in their function annotation are dynamically typed (operationsare checked at runtime). Code outside functions is statically typed bydefault, and types of variables are inferred. This does usually theright thing, but you can also make any variable dynamically typed bydefining it explicitly with the type Any:

  1. from typing import Any
  2.  
  3. s = 1 # Statically typed (type int)
  4. d: Any = 1 # Dynamically typed (type Any)
  5. s = 'x' # Type check error
  6. d = 'x' # OK