34
submitted 1 year ago* (last edited 1 year ago) by nikaro@jlai.lu to c/python@programming.dev

This is like Interface in Go (or Java, i don't speak Java but the article say so).

top 6 comments
sorted by: hot top controversial new old
[-] AlgoMammoth@programming.dev 6 points 1 year ago

Oh nice, I did not know about the Protocol class. It seems handy when I have to deal with untyped 3rd party modules.

[-] NostraDavid@programming.dev 1 points 1 year ago

OK, but why would I use this over ABCs? (Abstract Base Class). Or is it just different?

[-] bamboo@lemm.ee 3 points 1 year ago

Protocols are static duck typing. An object is a valid instance of protocol if it implements all the methods defined in the protocol, even if it doesn't declare it as implementing it. That last bit is important and the most distinguishing factor compared to an ABC.

[-] nikaro@jlai.lu 1 points 1 year ago

From what i understand, Protocol is for custom interfaces that you define (this object must have do_x() method), while ABCs are generic (this object is iterable).

[-] twoframesperminute@mastodon.social 0 points 1 year ago* (last edited 1 year ago)

@nikaro iterable means: has __iter__() method. So there's no real difference, as far as I can see.

[-] nikaro@jlai.lu 1 points 1 year ago

The difference is that with Protocol you can define which method presence you want to ensure. Like i said: custom vs. generic.

this post was submitted on 15 Sep 2023
34 points (97.2% liked)

Python

6343 readers
9 users here now

Welcome to the Python community on the programming.dev Lemmy instance!

📅 Events

PastNovember 2023

October 2023

July 2023

August 2023

September 2023

🐍 Python project:
💓 Python Community:
✨ Python Ecosystem:
🌌 Fediverse
Communities
Projects
Feeds

founded 1 year ago
MODERATORS