roam/20201007142751-python_typing.org
2021-07-29 18:51:25 -04:00

3.8 KiB
Raw Blame History

Python Typing

Python does not require nor enforce strict typing, and instead takes a dynamic approach using Duck Typing.

Gradual Typing support for Python is provided via type hints as described in PEP-484 (which covers Nominal Typing) and expanded upon in PEP-544 (which covers Structural Typing).

Variables, function arguments, and return values can have their types annotated, e.g. "variable x has type integer" or "function y takes a string and returns a list of dictionaries". While these are optional and not checked at run-time, they serve as documentation and can be used by various type checkers to ensure program correctness.

Available Type Checkers

Limitations

  • Type hints do not guarantee purity.
  • Type hints are not checked at run-time.

Fun facts

  • Per PEP-285, Python's boolean inherits from int, hence True + True = 2.