Python
typing.python.org › en › latest › spec › protocol.html
Protocols — typing documentation
The rationale for this is that the protocol class implementation is often not shared by subtypes, so the interface should not depend on the default implementation. Examples: from typing import Protocol class Template(Protocol): name: str # This is a protocol member value: int = 0 # This one too (with default) def method(self) -> None: self.temp: list[int] = [] # Error in type checker class Concrete: def __init__(self, name: str, value: int) -> None: self.name = name self.value = value def method(self) -> None: return var: Template = Concrete('value', 42) # OK
Mypy
mypy.readthedocs.io › en › stable › protocols.html
Protocols and structural subtyping - mypy 1.19.1 documentation
The collections.abc, typing and other stdlib modules define various protocol classes that correspond to common Python protocols, such as Iterable[T]. If a class defines a suitable __iter__ method, mypy understands that it implements the iterable protocol and is compatible with Iterable[T]. For example, IntList below is iterable, over int values:
Videos
09:16
Exploring Protocols in Python: Understanding & Creating Custom ...
10:10
You NEED to know about Python protocols - YouTube
Protocol Or ABC In Python - When to Use Which One?
15:31
Protocols vs ABCs in Python - When to Use Which One? - YouTube
08:32
What are "Protocols" In Python? (Tutorial 2023) - YouTube
Protocols in Python: Why You Need Them
Real Python
realpython.com › python-protocol
Python Protocols: Leveraging Structural Subtyping – Real Python
July 25, 2024 - For example, you can informally say that your Dog and Cat classes have a living protocol consisting of the .eat() and .drink() methods, which are the operations required to support life. You can also say that the classes have a sounding protocol comprised of the .make_sound() method. Note: In Python’s built-in classes, you’ll find many examples of classes that support multiple protocols.
Python
peps.python.org › pep-0544
PEP 544 – Protocols: Structural subtyping (static duck typing) | peps.python.org
Variable annotation syntax was added in Python 3.6, so that the syntax for defining protocol variables proposed in specification section can’t be used if support for earlier versions is needed. To define these in a manner compatible with older versions of Python one can use properties. Properties can be settable and/or abstract if needed: class Foo(Protocol): @property def c(self) -> int: return 42 # Default value can be provided for property...
Andrewbrookins
andrewbrookins.com › technology › building-implicit-interfaces-in-python-with-protocol-classes
Building Implicit Interfaces in Python with Protocol Classes – Andrew Brookins
So, why is this called a “protocol” and what kinds of things is it good for? Let’s go deeper to answer those questions. The built-in function len() works with any object that has a __len__() method. Objects don’t have to declare that they have a __len__() method or subclass any special classes to work with len(). Python programmers call this state of affairs a protocol, and the most common example is probably the iteration protocol.
Reddit
reddit.com › r/python › protocols vs abstract base classes in python
r/Python on Reddit: Protocols vs Abstract Base Classes in Python
December 1, 2024 -
Hi everyone. Last time I shared a post about Interface programming using abs in Python, and it got a lot of positive feedback—thank you!
Several people mentioned protocols, so I wrote a new article exploring that topic. In it, I compare protocols with abstract base classes and share my thoughts and experiences with both. You can check it out here: https://www.tk1s.com/python/protocols-vs-abstract-base-classes-in-python Hope you'll like it! Thanks!
Top answer 1 of 5
14
I'm going to add that performance for protocols is bad when doing an instance check. It's O(n) where n is the size of the interface, and they're not cheap checks. For example, I improved performance in ReactPy by 50% by removing a single isinstance check against a protocol that was done every render.
2 of 5
12
https://github.com/dabeaz/blog/blob/main/2021/barely-interface.md
Python
typing.python.org › en › latest › reference › protocols.html
Protocols and structural subtyping — typing documentation
The typing module defines various protocol classes that correspond to places where duck typing is commonly used in Python, such as Iterable[T].
Python.org
discuss.python.org › ideas
@typing.protocol decorator to create a class of type Protocol - Ideas - Discussions on Python.org
October 5, 2022 - Instead of having typing.Protocol, which we inherit to define a Protocol class, we could have a global @protocol decorator that declares a class as a Protocol. And the class that adheres to that Protocol should be decorated with a decorator having the name of the Protocol class it adheres to.
Python Tutorial
pythontutorial.net › home › python oop › python protocol
Python Protocol
March 31, 2025 - class Item(Protocol): quantity: float price: floatCode language: Python (python)
Stephensugden
stephensugden.com › crash_into_python › Protocols.html
Protocols
The secret is the iteration protocol. As long as some_collection implements a couple of methods it can be used as an iterator. http://docs.python.org/library/stdtypes.html#iterator-types · def reverse_iter(seq): position = len(seq) - 1 while True: if position < 0: raise StopIteration yield seq[position] position -= 1 class BackwardsSequence(list): def __iter__(self): return reverse_iter(self)
Idego Group
idego-group.com › other › we need to talk about protocols in python
We need to talk about Protocols in Python | Idego Group
February 22, 2023 - It means that if two objects have the same methods and attributes, Python will treat them as the same type. For comparison, ABCs use nominal typing, where object relationship is defined by inheritance stated deliberately in our class definition (e.g. class B(A)). Protocols are, therefore, interfaces which help define what is expected as an input of our methods.