Whereas with Protocols it's gonna be ( good tutorial ): I think that is not a good example of how to write programs. What he did by having protocols I would have done by using mixins. The way that I see objects is that they have various capabilities that can be mixed in. multiple inheritance in python would have been a much better way to implement that example in my opinion. I would also say that the author of this tutorial needs to learn a thing or 2 about an inversion of control and dependency injection. The author basically sets up a straw man problem and then solves his straw man problem. He had no business creating instances of the object outside of the class itself. If he had simply called a constructor methods within the classes then the other class wouldn't have been attempting to make instances of those other classes. Answer from thedeepself on reddit.com
🌐
Python
typing.python.org › en › latest › spec › protocol.html
Protocols — typing documentation
from typing import Protocol from abc import abstractmethod class Example(Protocol): def first(self) -> int: # This is a protocol member return 42 @abstractmethod def second(self) -> int: # Method without a default implementation raise NotImplementedError
🌐
Justin A. Ellis
jellis18.github.io › post › 2022-01-11-abc-vs-protocol
Abstract Base Classes and Protocols: What Are They? When To Use Them?? Lets Find Out! - Justin A. Ellis
January 11, 2022 - Note that this looks pretty similar to our ABC based Animal class above. We inherit from typing.Protocol instead of abc.ABC and we don't need to add the @abstractmethod decorators since Protocols are not meant to be "implemented" but simply act as an interface in downstream tasks.
🌐
Python.org
discuss.python.org › typing
Calling abstract methods - Typing - Discussions on Python.org
January 6, 2024 - PEP 544 indicates that a type checker should generate an error if a class that explicitly derives from the protocol attempts to call a method through super() if that method is unimplemented in the protocol. class Proto(Protocol): def method(self) -> None: ... class Impl(Proto): def method(self) -> None: super().method() # Type checker error This makes sense because the method in the protocol is effectively abstract.
🌐
Python
peps.python.org › pep-0544
PEP 544 – Protocols: Structural subtyping (static duck typing) | peps.python.org
from typing import Protocol from abc import abstractmethod class Example(Protocol): def first(self) -> int: # This is a protocol member return 42 @abstractmethod def second(self) -> int: # Method without a default implementation raise NotImplementedError
🌐
Medium
medium.com › @pouyahallaj › introduction-1616b3a4a637
Python Protocols vs. ABCs: A Comprehensive Comparison of Interface Design | Medium
May 29, 2023 - To define an ABC in Python, you can make use of the abc module: from abc import ABC, abstractmethod class MyABC(ABC): @abstractmethod def my_method(self): pass
🌐
Python
docs.python.org › 3 › library › abc.html
abc — Abstract Base Classes
The abstractmethod() only affects subclasses derived using regular inheritance; “virtual subclasses” registered with the ABC’s register() method are not affected.
🌐
Reddit
reddit.com › r/python › interfaces with protocols: why not ditch abc for good?
r/Python on Reddit: Interfaces with Protocols: why not ditch ABC for good?
January 22, 2023 -

Hello, if one finds interfaces useful in Python (>=3.8) and is convinced that static type-checking is a must, then why not ditch ABC and always use Protocols? I understand that the fundamental idea of a protocol is slightly different from an interface, but in practice, I had great success replacing abc's with Protocols without regrets.

With abc you would write (https://docs.python.org/3/library/abc.html) :

from abc import ABC, abstractmethod

class Animal(ABC):
   @abstractmethod
   def eat(self, food) -> float:
       pass

Whereas with Protocols it's gonna be (good tutorial):

from typing import Protocol

class Animal(Protocol):
   def eat(self, food) -> float:
       ...

Scores in my subjective scoring system :)

CapabilityABCProtocols
Runtime checking11 (with a decorator)
Static checking with mypy11
Explicit interface (class Dog(Animal):)11
Implicit interface with duck-typing (class Dog:)0.5 (kind of with register, but it doesn't work with mypy yet)1
Default method implementation (def f(self): return 5)-1 (implementations shouldn't be in the interfaces)-1 (same, and mypy doesn't catch this)
Callback interface01
Number of code lines-1 (requires ABC inheritance and abstracmethod for every method)0 (optionalProtocol inheritance)
Total score1.54

So I do not quite see why one should ever use ABC except for legacy reasons. Other (IMHO minor) points in favour of ABC I've seen were about interactions with code editors.

Did I miss anything?

I put more detailed arguments into a Medium. There are many tutorials on using Protocols, but not many on ABC vs Protocols comparisons. I found a battle of Protocols vs Zope, but we are not using Zope, so it's not so relevant.

🌐
DEV Community
dev.to › meseta › factories-abstract-base-classes-and-python-s-new-protocols-structural-subtyping-20bm
Python's new Protocols (Structural subtyping), Abstract Base Classes, and Factories - DEV Community
August 18, 2020 - Python has a built-in library for this called abc which stands for Abstract Base Class. The idea is to define an abstract base class for the file handler, against which new concrete implementations of different file handlers can be built.
Find elsewhere
🌐
Real Python
realpython.com › ref › glossary › abstract-method
abstract method | Python Glossary – Real Python
You mark abstract methods with the @abstractmethod decorator from the abc module. You use them to define a common interface that all concrete subclasses must implement. If any abstract methods remain unimplemented in a subclass, Python prevents ...
🌐
Sinavski
sinavski.com › home › interfaces abc vs. protocols
Interfaces: abc vs. Protocols - Oleg Sinavski
August 1, 2021 - But recently, I found that relatively new Python Protocols are way nicer. People find uses for both technologies. But I want to convince you to completely jump ships and start using them instead of more traditional techniques. Python is somewhat different from other popular languages since there are no interfaces on a language level. However, there are several library implementations: ... from abc import ABC, abstractmethod class Animal(ABC): @abstractmethod def eat(self, food) -> float: pass @abstractmethod def sleep(self, hours) -> float: pass
🌐
GeeksforGeeks
geeksforgeeks.org › python › abstract-classes-in-python
Abstract Classes in Python - GeeksforGeeks
Python provides the abc module to define ABCs and enforce the implementation of abstract methods in subclasses. Example: This example shows an abstract class Animal with an abstract method sound() and a concrete subclass Dog that implements it. ... from abc import ABC, abstractmethod class Animal(ABC): @abstractmethod def sound(self): pass class Dog(Animal): def sound(self): return "Bark" dog = Dog() print(dog.sound())
Published   September 3, 2025
🌐
Real Python
realpython.com › python-protocol
Python Protocols: Leveraging Structural Subtyping – Real Python
July 25, 2024 - @classmethod def class_method(cls) -> str: ... @staticmethod def static_method(arg: int) -> str: ... @property def property_name(self) -> str: ... @property_name.setter def property_name(self, value: str) -> None: ... @abstractmethod def abstract_method(self) -> str: ... You use ClassVar to define a class attribute. Then, you have an instance attribute with a default value, which is optional. It’s important to note that instance attributes must be declared at the class level for the type checker to consider them a part of the protocol. Otherwise, if you define instance attributes inside instance methods, which is a common practice in Python, then you’ll get an error from your type checker.
Top answer
1 of 2
7

I've used this before in cases where it was possible to have the concrete implementation, but I wanted to force subclass implementers to consider if that implementation is appropriate for them.

One specific example: I was implementing an abstract base class with an abstract factory method so subclasses can define their own __init__ function but have a common interface to create them. It was something like

class Foo(ABC):
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

    @classmethod
    @abstractmethod
    def from_args(cls, a, b, c) -> "Foo":
        return cls(a, b, c)

Subclasses usually only need a subset of the arguments. When testing, it's cumbersome to access the framework which actually uses this factory method, so it's likely someone will forget to implement the from_args factory function since it wouldn't come up in their usual testing. Making it an abstractmethod would make it impossible to initialize the class without first implementing it, and will definitely come up during normal testing.

2 of 2
4

The short answer is: Yes.

As described in the Python Documentation of abc:

The abstract methods can be called using any of the normal ‘super’ call mechanisms

PEP3119 also discussed this behavior, and explained it can be useful in the super-call:

Unlike Java’s abstract methods or C++’s pure abstract methods, abstract methods as defined here may have an implementation. This implementation can be called via the super mechanism from the class that overrides it. This could be useful as an end-point for a super-call in framework using cooperative multiple-inheritance [7], [8].

Actually, what the @abstractmethod decorator all do is setting the function's __isabstractmethod__ attribute to True (you can see the implementation code here). And the cooperation of both ABC and @abstractmethod can prevent the instantiation of an abstract class.

So, you can do whatever a normal method can do in an abstract method.

🌐
GitHub
gist.github.com › Integralist › cc04c2c34a988be26e56fe2f3ea95aff
[Python Interfaces via Protocols and Abstract Base Classes (with Metaclasses)] #python #interfaces #protocols #design #collections #abc #iterator #sized #metaclasses #abstract · GitHub
Protocols are useful but sometimes you require something that does indeed behave more like a traditional 'interface', and that's where ABCs can help us. They are best explained by way of an example: import abc class Foo(abc.ABC): @abc.abstractmethod def bar(self): pass class Thing(Foo): pass t = Thing() # TypeError: Can't instantiate abstract class Thing with abstract methods bar ·
🌐
Reddit
reddit.com › r/python › python interfaces: choose protocols over abc
r/Python on Reddit: Python Interfaces: Choose Protocols Over ABC
February 12, 2023 - --- If you have questions or are new to Python use r/LearnPython ... Archived post. New comments cannot be posted and votes cannot be cast. Share ... Protocols and abstract classes are not interchangeable.
🌐
GitHub
github.com › pylint-dev › pylint › issues › 7209
false positive: abstract-method for typing.Protocol inheritance with abc.abstractmethod · Issue #7209 · pylint-dev/pylint
"""FooBar Protocol""" # pylint: disable=too-few-public-methods,disallowed-name from abc import abstractmethod from typing import Protocol, Literal class FooProtocol(Protocol): """Foo Protocol""" @abstractmethod def foo(self) -> Literal["foo"]: # pylint: disable=invalid-name """foo method""" class BarProtocol(Protocol): """Bar Protocol""" @abstractmethod def bar(self) -> Literal["bar"]: """bar method""" class FooBarProtocol(FooProtocol, BarProtocol, Protocol): """FooBar Protocol""" class FooBar(FooBarProtocol): """FooBar object""" def bar(self) -> Literal["bar"]: return "bar" def foo(self) -> Literal["foo"]: return "foo"
🌐
TypeThePipe
typethepipe.com › post › python-protocols-when-to-use
Python protocols. When to use them in your projects to abstract and decoupling | TypeThePipe
December 11, 2023 - Let’s create an example on how to create API where an Explainable protocol · from pydantic import BaseModel from typing import List, Optional, Tuple import numpy as np import matplotlib.pyplot as plt import matplotlib.figure from abc import ABC class BaseScorer(ABC, BaseModel): @abstractmethod def predict(self): ...
🌐
GitConnected
levelup.gitconnected.com › python-interfaces-choose-protocols-over-abc-3982e112342e
Python interfaces: abandon ABC and switch to Protocols | by Oleg Sinavski | Level Up Coding
January 19, 2023 - But recently, I found that relatively new Python Protocols are way nicer. People find uses for both technologies. But I want to convince you to completely jump ships and start using them instead of more traditional techniques. Python is somewhat different from other popular languages since there are no interfaces on a language level. But there are several library implementations. The abc package is probably the most popular: from abc import ABC, abstractmethod class Animal(ABC): @abstractmethod def eat(self, food) -> float: pass @abstractmethod def sleep(self, hours) -> float: pass
🌐
Zero To Mastery
zerotomastery.io › blog › abstract-classes-in-python
Beginner’s Guide to Abstract Classes in Python | Zero To Mastery
from abc import ABC, abstractmethod class Payment(ABC): @abstractmethod def authorize(self): pass @abstractmethod def process(self): pass At this point, you’ve created a blueprint. You’re basically saying: “Any class that inherits from this must include BOTH of these methods.” · But because it’s just a blueprint, Python won’t let you create a Payment() object directly, so now let’s create a real payment class that actually works: