In >= 3.8, this code raises "TypeError: Cannot inherit from plain Generic":

class Foo(typing.Generic):

but this one works:

class Protocol(typing.Generic):

And in Python 3.7 it works if you call it _Protocol instead

Show thread

@bram @val Dug up the git blame to find this PEP:

Seems intended to extend "nominal subtyping" to "structural subtyping (static duck typing)". I don't understand what it means from the abstract... It's a long PEP. Maybe something for later.

@ashwinvis @bram Yeah, Protocols are cool. Basically it's like a Rust trait or a C++ interface; a class is said to implement a Protocol if it implements all the methods in it (it doesn't even need to inherit it to be considered a subclass).

For example, Iterable is a Protocol, which requires classes to implement a __iter__ method.

Sign in to participate in the conversation
Octodon is a mostly French-speaking Mastodon instance with an active moderation. est une instance Mastodon principalement francophone et avec une modération active.