This is like Interface in Go (or Java, i don’t speak Java but the article say so).

  • AlgoMammoth
    link
    fedilink
    arrow-up
    6
    ·
    1 year ago

    Oh nice, I did not know about the Protocol class. It seems handy when I have to deal with untyped 3rd party modules.

  • NostraDavid
    link
    fedilink
    arrow-up
    1
    ·
    1 year ago

    OK, but why would I use this over ABCs? (Abstract Base Class). Or is it just different?

    • bamboo@lemm.ee
      link
      fedilink
      arrow-up
      4
      ·
      1 year ago

      Protocols are static duck typing. An object is a valid instance of protocol if it implements all the methods defined in the protocol, even if it doesn’t declare it as implementing it. That last bit is important and the most distinguishing factor compared to an ABC.

    • nikaro@jlai.luOP
      link
      fedilink
      arrow-up
      1
      ·
      1 year ago

      From what i understand, Protocol is for custom interfaces that you define (this object must have do_x() method), while ABCs are generic (this object is iterable).