Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

General protocols

These protocols cover iteration, comparison, operators, and hashing.

Iteration

  • Iterable[A]: values that can produce an iterator with __iter__ and can therefore be used in for loops and other iteration-based APIs

Identity and comparison

  • Identity: identity comparison with is and is not
  • Eq: equality comparison with == and !=
  • Ord (Eq): ordering with <, <=, >, and >=

If you see a constraint such as A(Ord) in a type signature, it means values of type A support ordering.

Operator families

  • Logical: &, |, ^, and their in-place forms
  • Plus: +, +=, and __zero__
  • Minus: - and -=
  • Times[A] (Plus): * and *=, with right-hand operand type A
  • Div[A]: / and /=, with result type A

Hashing

  • Hashable (Eq): values that can feed data into a hasher; required for dictionary keys and set elements

If you see a constraint such as A(Hashable), values of type A can be used where hashing is required.

See Hashable for the full protocol reference and an implementation example.