35
Type hinting in modern Python: The Protocol class
(codebeez.nl)
Welcome to the Python community on the programming.dev Lemmy instance!
Past
November 2023
October 2023
July 2023
August 2023
September 2023
OK, but why would I use this over ABCs? (Abstract Base Class). Or is it just different?
From what i understand,
Protocol
is for custom interfaces that you define (this object must havedo_x()
method), while ABCs are generic (this object is iterable).@nikaro iterable means: has __iter__() method. So there's no real difference, as far as I can see.
The difference is that with
Protocol
you can define which method presence you want to ensure. Like i said: custom vs. generic.