nikaro@jlai.lu to Python@programming.dev · edit-21 year agoType hinting in modern Python: The Protocol classcodebeez.nlexternal-linkmessage-square6fedilinkarrow-up135arrow-down11file-text
arrow-up134arrow-down1external-linkType hinting in modern Python: The Protocol classcodebeez.nlnikaro@jlai.lu to Python@programming.dev · edit-21 year agomessage-square6fedilinkfile-text
minus-squarenikaro@jlai.luOPlinkfedilinkarrow-up1·1 year agoFrom 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).
minus-squaretwoframesperminute@mastodon.sociallinkfedilinkarrow-up0·edit-21 year ago@nikaro iterable means: has __iter__() method. So there’s no real difference, as far as I can see.
minus-squarenikaro@jlai.luOPlinkfedilinkarrow-up1·1 year agoThe difference is that with Protocol you can define which method presence you want to ensure. Like i said: custom vs. generic.
From what i understand,
Protocol
is for custom interfaces that you define (this object must havedo_x()
method), while ABCs are generic (this object is iterable).@nikaro iterable means: has __iter__() method. So there’s no real difference, as far as I can see.
The difference is that with
Protocol
you can define which method presence you want to ensure. Like i said: custom vs. generic.