Protocols in Python

Pavol Kutaj
1 min readAug 31, 2021

The aim of this page📝is to conceptualize protocols in python

1. define

  • protocol is a set of operations that a type must support to implement
  • they do not need to be defined as separate interfaces or base classes
  • that would be the case in nominally typed languages such as C# or Java
  • it is enough to provide functioning implementations of these operations
  • protocol support demands specific behavior

2. container protocol requirements

  • requirement: membership testing using in and not in

3. sized protocol requirements

  • requirement: length of a type by checking with len(container)

4. iterable protocol requirements

  • yield items one by one as they are requested
  • iterable is an object from which you can fetch a sequence of other objects
  • the act of fetching from an iterable is known as iteration
  • they can be used in for loops

5. sequence protocol requirements

  • items can be retrieved using []
  • items can be searched-for with sequence.idex(item)
  • items can be counted with sequence.count()
  • reversed copy can be produced with reversed(sequence)
  • must support iterable + sized + container



Pavol Kutaj

Today I Learnt | Infrastructure Support Engineer at with a passion for cloud infrastructure/terraform/python/docs. More at