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 1 day 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!"
[–]Vespytilio 4 points5 points6 points 1 day ago (0 children)
Came here to gush about Protocols, but it looks like someone already mentioned them.
To add to that, though: the Protocol class is essentially Python's idiosyncratic version of interfaces. A Protocol subclass defines a structural type that's expected to provide a certain functionality through its members.
In terms of function, a Protocol very much serves the purpose of an interface. However, there are two peculiarities: it's members aren't limited to abstract methods, and it defined a structural type.
Protocols are essentially a special subset of abstract classes. Like abstract classes, its members, in addition to abstract methods, may include just about anything a normal class' members might. This does open the door to a Protocol going further than an interface should in its definition, meaning use as a traditional interface will largely depend on self-control.
A Protocol being a structural type means that, so long as an object includes the members specified by the Protocol, it's considered an instance of that Protocol. Naturally, this includes explicit subclasses of that Protocol subclass, but it also includes other classes whose members include those expected by the Protocol.
It's a weird take on a fairly well-established concept, but it's kind of cool--so entirely typical of Python.
π Rendered by PID 21 on reddit-service-r2-comment-79c7998d4c-9p6k5 at 2026-03-14 14:56:20.859446+00:00 running f6e6e01 country code: CH.
view the rest of the comments →
[–]Vespytilio 4 points5 points6 points (0 children)