4 ms·
I'm very curious to hear where people stand on Protocols vs ABCs. While I definitely see the appeal of Protocols, I _think_ I prefer ABCs. Realistically it prob
by yeswecatan 5y ago
I'm very curious to hear where people stand on Protocols vs ABCs. While I definitely see the appeal of Protocols, I _think_ I prefer ABCs. Realistically it probably depends on the situation.
- DandyDev 5y agoArticle author here. To me the big advantage of Protocols as opposed to ABCs, is that you don't have to have your classes implement a special base class and can instead focus on the _consuming_ code. Using a Protocol for a function parameter works even for passing in objects of classes you don't control.
- pgorczak 5y agoI think it boils down to thinking in composition (x has a y) vs inheritance (x is a y). I’m happy about protocols because most of the times, coming up with some pattern or order of inheritance using ABCs really feels beside the point of what I’m trying to achieve (stating that an object _has_ a specific set of attributes).
- gjulianm 5y agoIn my case I mostly use ABCs, not because I think they're universally better but because my use case fits them: dealing mostly with my own types, base classes provide some common functionality and the number of types is limited. Protocols are great for the opposite cases: you use types that are not necessarily under your control, no shared functionality and a wider amount of types.
- formerly_proven 5y agoABCs lend themselves well to create inheritance-heavy code with hierarchies, which tends to become rather messy quick.