this post was submitted on 13 Jul 2023
11 points (82.4% liked)

Python

6580 readers
132 users here now

Welcome to the Python community on the programming.dev Lemmy instance!

πŸ“… Events

PastNovember 2023

October 2023

July 2023

August 2023

September 2023

🐍 Python project:
πŸ’“ Python Community:
✨ Python Ecosystem:
🌌 Fediverse
Communities
Projects
Feeds

founded 2 years ago
MODERATORS
top 8 comments
sorted by: hot top controversial new old
[–] [email protected] 5 points 2 years ago (1 children)

I need to move away from using inheritance in my Python. I've been using Rust exclusively for the past year and it's definitely going to affect how I write code in other languages.

[–] [email protected] 4 points 2 years ago (1 children)

"If only I could make this a trait..."

[–] [email protected] 3 points 2 years ago* (last edited 2 years ago) (1 children)

Abc and Protocols are not the same, though. The protocol is met when all methods are implemented with the expected signature. Abstract base classes require you to opt in to be considered an Implementation of some interface. Both have different use cases. Protocols are much more fragile.

[–] [email protected] 2 points 2 years ago (1 children)

I wouldn't say Protocols are more "fragile"... They're more strict.

[–] [email protected] 1 points 2 years ago

The opposite is true. If you have two interfaces that contain methods with the same name, then they have the same typing.Protocol. It is not possible to specify preconditions or contracts, as you would with abc.

[–] [email protected] -1 points 2 years ago* (last edited 2 years ago)

No. Duck types (including virtual subclasses) considered harmful; use real inheritance if your language doesn't provide anything strictly better.

It is incomparably convenient to be able to retroactively add "default implementations" to interface functions (consider for example how broken readinto is in Python). Some statically-typed languages let you do that without inheritance, but no dynamically-typed language can.

This reads more as a rant against inheritance (without any explanation whatsoever) than a legitimate argument.

load more comments
view more: next β€Ί