Python
typing.python.org › en › latest › spec › protocol.html
Protocols — typing documentation
Type checkers should reject an isinstance() or issubclass() call, if there is an unsafe overlap between the type of the first argument and the protocol. Type checkers should be able to select a correct element from a union after a safe isinstance() or issubclass() call. For narrowing from non-union types, type checkers can use their best judgement (this is intentionally unspecified, since a precise specification would require intersection types). ... © Copyright 2021, The Python Typing Team.
Real Python
realpython.com › python-protocol
Python Protocols: Leveraging Structural Subtyping – Real Python
July 25, 2024 - Now, your classes are tightly coupled, even if they don’t have a clear inheritance relationship. How can you fix this collision between duck typing and type hints? This is where protocols come into the scene. In Python’s type system, you’ll find two ways to decide whether two objects are compatible as types:
Videos
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
Protocol Buffers
protobuf.dev › getting-started › pythontutorial
Protocol Buffer Basics: Python | Protocol Buffers Documentation
Use the protocol buffer compiler. Use the Python protocol buffer API to write and read messages.
Xebia
xebia.com › home › blog › protocols in python: why you need them
Protocols In Python: Why You Need Them | Xebia
July 25, 2022 - The protocol class implementation defines rules and guidelines for implementing protocol methods within a class, including the use of variable annotations. Python Protocol class variables are defined within the class body of a protocol, and there is a distinction between protocol class variables and protocol instance variables.
mypy
mypy.readthedocs.io › en › latest › protocols.html
Protocols and structural subtyping - mypy 1.20.0+dev.33f4ae307403a664c7b88a225a3074b572978682 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]. ...
Sarahabd
sarahabd.com › sarah abderemane's website › til › python protocol typing
TIL: python Protocol and typing
December 18, 2024 - To conclude with, Protocols provide a way to define structural typing in Python, allowing you to create interfaces without the need for explicit inheritance.
Andrewbrookins
andrewbrookins.com › technology › building-implicit-interfaces-in-python-with-protocol-classes
Building Implicit Interfaces in Python with Protocol Classes – Andrew Brookins
July 5, 2020 - 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.
Pybites
pybit.es › articles › typing-protocol-abc-alternative
Leveraging typing.Protocol: Faster Error Detection And Beyond Inheritance – Pybites
February 9, 2024 - Shortly after I learned that you can do this as well with protocols. Python 3.8 introduced quite a groundbreaking feature that further advanced the language's capabilities in type checking: the typing.Protocol which allows Python developers to define and enforce interface contracts in…
GitHub
github.com › modelcontextprotocol › python-sdk
GitHub - modelcontextprotocol/python-sdk: The official Python SDK for Model Context Protocol servers and clients · GitHub
Starred by 22.2K users
Forked by 3.2K users
Languages Python
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]. ...
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.
Hacker News
news.ycombinator.com › item
Interfaces and Protocols in Python | Hacker News
March 20, 2021 - Highly recommend reading: · https://www.python.org/dev/peps/pep-0544/