• rglullis@communick.newsOP
    link
    fedilink
    English
    arrow-up
    5
    ·
    1 year ago

    The key part is this:

    it turns out protocols really are just a formalization of Python’s duck typing.

    Meaning, this is just a way to say that if you are defining some system that needs to conform with some interface, you can have type checked even if your have different objects from different classes. No need for TypeVar or define a crazy hierarchy: as long as the types implements the methods defined by the Protocol, the type checker will be happy.

    • bleistift2@feddit.de
      link
      fedilink
      English
      arrow-up
      1
      arrow-down
      7
      ·
      edit-2
      1 year ago

      It’s amazing how often people celebrate some new feature in a language and I’m like: TypeScript has been doing this for years now.

      [Edit: This is also how interfaces work in Go, and it’s just the old advice “Code against interfaces, not classes.”]

      • lysdexic@programming.dev
        link
        fedilink
        English
        arrow-up
        2
        ·
        1 year ago

        I’s amazing how often people celebrate some new feature in a language and I’m like: TypeScript has been doing this for years now.

        Whatever another programming language supports or does is entirely irrelevant if what you’re working with is Python.