• @o11c@programming.dev
    link
    fedilink
    English
    011 months ago

    In practice, Protocols are a way to make “superclasses” that you can never add features to (for example, readinto despite being critical for performance is utterly broken in Python). This should normally be avoided at almost all costs, but for some reason people hate real base classes?

    If you really want to do something like the original article, where there’s a C-implemented class that you can’t change, you’re best off using a (named) Union of two similar types, not a Protocol.

    I suppose they are useful for operator overloading but that’s about it. But I’m not sure if type checkers actually implement that properly anyway; overloading is really nasty in a dynamically-typed language.

    • @lysdexic@programming.dev
      link
      fedilink
      111 months ago

      In practice, Protocols are a way to make “superclasses” that you can never add features to (for example, readinto despite being critical for performance is utterly broken in Python).

      Not really, and this interpretation is oblivious to the concept of protocols and misses the whole point of them.

      The point of a protocol is to specify that a type supports a specific set of methods with specific signatures, aka duck typing, and provide the necessary and sufficient infrastructure to check if objects comply with a protocol and throw an error in case it doesn’t.

      Also, protocol classes can be inherited, and protocols can be extended.

      https://peps.python.org/pep-0544/

      • @o11c@programming.dev
        link
        fedilink
        111 months ago

        Then - ignoring dunders that have weird rules - what, pray tell, is the point of protocols, other than backward compatibility with historical fragile ducks (at the cost of future backwards compatibility)? Why are people afraid of using real base classes?

        The fact that it is possible to subclass a Protocol is useless since you can’t enforce subclassing, which is necessary for maintainable software refactoring, unless it’s a purely internal interface (in which case the Union approach is probably still better).

        That PEP link includes broken examples so it’s really not worth much as a reference.

        (for that matter, the Sequence interface is also broken in Python, in case you need another historical example of why protocols are a bad idea).

        • @lysdexic@programming.dev
          link
          fedilink
          English
          1
          edit-2
          11 months ago

          (…) what, pray tell, is the point of protocols, other than backward compatibility with historical fragile ducks (at the cost of future backwards compatibility)?

          Got both of those wrong. The point of protocols is to have a way to validate duck typing errors by adding a single definition of a duck. This is not something that only applies to backwards compatible, nor does it affects backwards compatibility.

          Why are people afraid of using real base classes?

          You’re missing the whole point of prototypes. The point of a prototype is that you want duck typing, not inheritance. Those are entirely different things, and with prototypes you only need to specify a single prototype and use it in a function definition, and it automatically validates each and any object passed to it without having to touch it’s definition or add a base class.

          • @o11c@programming.dev
            link
            fedilink
            111 months ago

            That still doesn’t explain why duck typing is ever a thing beyond “I’m too lazy to write extends BaseClass”. There’s simply no reason to want it.

            • @lysdexic@programming.dev
              link
              fedilink
              English
              111 months ago

              I already explained it to you: protocols apply to types you do not own or control, let alone can specify a base class.

              You just specify a protocol, specify that a function requires it, and afterwards you can pass anything to it as-is and you’ll be able to validate your calls.

                • @lysdexic@programming.dev
                  link
                  fedilink
                  English
                  1
                  edit-2
                  11 months ago

                  I’m not sure you understand that what a union does or does not do is completely irrelevant and besides the point. Python’s protocols add support for structural subtyping, and enable both runtime and build-time type checks without requiring major code changes. Don’t you understand what that means?