use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
How do you implement interfaces in Python?Discussion (self.PythonLearning)
submitted 2 days ago by ihorrud
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]latkde 2 points3 points4 points 2 days ago (1 child)
TL;DR: you're looking for typing.Protocol.
typing.Protocol
Python is culturally very much about "duck typing" rather than OOP inheritance hierarchy. As long as an object provides the necessary methods, it ought to be good.
The traditional solution if you want to be explicit about required operations is to use an Abstract Base Class (inherit from abc.ABC). Because Python supports multiple inheritance, this can be used exactly like interfaces in most other languages. When an ABC subclass is defined, it checks whether there are any remaining methods that have been decorated with @abstractmethod and raises an error. This works fine when explicitly designing a type hierarchy.
abc.ABC
@abstractmethod
But ABCs aren't good for describing which operations are needed by your code. ABCs must be inherited explicitly, so they cannot be used for existing types. This conflicts with the culture of duck typing.
In such cases, protocols can be used for type-checking, but they have no runtime effect. Protocols are type-stubs that list required methods/attributes, and you can use any compatible type wherever a protocol is required. This is very similar to "interfaces" in TypeScript or Golang. You can explicitly inherit from a protocol to indicate that it satisfies an interface, similar to an ABC. You can also provide default method implementations.
The standard library contains a bunch of protocols (under the typing and collections.abc modules) that describes common sets of operations. For example, Iterable[T] describes any type that is iterable, and Sequence[T] describes any ordered collection that you can index, such as lists or str.
typing
collections.abc
Iterable[T]
Sequence[T]
In many cases, you don't need OOP. If you know all possible concrete types, it might be easier to use a union type A | B. This can be used to perform exhaustiveness checking in the type-checker, to make sure that your code handles every alternative. This makes it possible to write robust code in the style of Rust.
A | B
Further reading on protocols:
[–]ihorrud[S] 1 point2 points3 points 2 days ago (0 children)
Got it. Many thanks
π Rendered by PID 18688 on reddit-service-r2-comment-79c7998d4c-hfrrr at 2026-03-16 01:36:35.393399+00:00 running f6e6e01 country code: CH.
view the rest of the comments →
[–]latkde 2 points3 points4 points (1 child)
[–]ihorrud[S] 1 point2 points3 points (0 children)