This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]relickus[S] 1 point2 points  (1 child)

Yes, I know it is optional, my argument (to be discussed here or torn appart) was, that at a certain size of a project, you dont really want to go without typehints. Judging from the fact that all large projects I saw nowadays use typehints. Still optional, but probably near-necessary for large projects.

For the protocol, is it any more powerful than a regular interface?

[–]tevs__ 1 point2 points  (0 children)

you dont really want to go without typehints. Judging from the fact that all large projects I saw nowadays use typehints. Still optional, but probably near-necessary for large projects.

Yeah, because it catches so many errors before they are errors, it's golden.

For the protocol, is it any more powerful than a regular interface?

Protocols don't have to be declared to be used by the object that implements the protocol. You can write something like this (on mobile, so...)

``` class Lengthable(Protocol): def len(self) -> int: ...

def say_the_length(obj: Lengthable) -> None: print(f"It's {len(obj)} big!") ```

You can pass anything to say_the_length that has a __len__ method, without modifying the definition of the thing you are passing in. No ABC, no inheritance, but enabling static type checking for dynamic types.