🌐
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:
🌐
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!

🌐
Python
peps.python.org › pep-0544
PEP 544 – Protocols: Structural subtyping (static duck typing) | peps.python.org
In this PEP we specify static and runtime semantics of protocol classes that will provide a support for structural subtyping (static duck typing). Currently, PEP 484 and the typing module [typing] define abstract base classes for several common Python protocols such as Iterable and Sized.
🌐
Simon Willison
til.simonwillison.net › python › protocols
Protocols in Python | Simon Willison’s TILs
July 26, 2023 - That reveal_type(row) line will raise an error if you run the code using python and not mypy. The fix for that looks like this: from typing import TYPE_CHECKING ... if TYPE_CHECKING: reveal_type(obj) PEP 544 – Protocols: Structural subtyping (static duck typing)
🌐
DEV Community
dev.to › shameerchagani › what-is-a-protocol-in-python-3fl1
What is a Protocol in python? - DEV Community
July 5, 2023 - In Python, protocols refer to a concept introduced in Python 3.8 as a way to define structural typing or "duck typing" within the language. A protocol is a set of methods or attributes that an object must have in order to be considered compatible ...
🌐
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.
Find elsewhere
🌐
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.
🌐
DEV Community
dev.to › fwojciec › protocols-and-composition-in-python-8mm
Protocols and Composition in Python - DEV Community
November 6, 2021 - A protocol is Python's take on "structural subtyping" - it's a type that's effectively implemented by anything that matches the signature of the protocol's methods. Concrete implementations can subclass the protocol, in which case implementation ...
🌐
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…
🌐
Sinavski
sinavski.com › home › interfaces abc vs. protocols
Interfaces: abc vs. Protocols - Oleg Sinavski
August 1, 2021 - A protocol is a formalization of Python’s “duck-typing” ideology. There are many great articles on structural typing in Python (this, that and some discussion). Maybe protocols and interfaces are theoretically different beasts, but a protocol does the job.
🌐
Medium
medium.com › @commbigo › python-typing-protocol-c2f6a60c0ac6
Python typing — Protocol
December 28, 2023 - Python typing — Protocol We all know typing in python becomes important. Now, we introduce protocol defined in pep-544 and release in python 3.8. Sometimes, we hit the exception “AttributeError …
🌐
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/
🌐
Python
docs.python.org › 3 › library › asyncio-protocol.html
Transports and Protocols — Python 3.14.3 documentation
There is always a 1:1 relationship between transport and protocol objects: the protocol calls transport methods to send data, while the transport calls protocol methods to pass it data that has been received.