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.
"If only I could make this a trait
..."
Yes!
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.
I wouldn't say Protocols are more "fragile"... They're more strict.
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.
Personally I prefer functional architectures + callbacks (better) / closures (tolerable),they tend to scale a bit better.
If a a dog needs to eat, then invoke the eat function with dog data and the dog eat function.
Explicit (this data is dog so I feed it with the dog eat function) vs implicit (I have no idea, I ask my data to eat as it pleases).
Explicit design tends to have fewer bugs my conjecture. Protocols are a halfway point, at least you know “hey this thing eats … uh … something.”
However, if doing that, just take it all the way - systems are better explicitly linking a verb to a noun vs assuming the noun knows how to verb.
It is also easier to test and debug.
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.
Python
Welcome to the Python community on the programming.dev Lemmy instance!
📅 Events
Past
November 2023
- PyCon Ireland 2023, 11-12th
- PyData Tel Aviv 2023 14th
October 2023
- PyConES Canarias 2023, 6-8th
- DjangoCon US 2023, 16-20th (!django 💬)
July 2023
- PyDelhi Meetup, 2nd
- PyCon Israel, 4-5th
- DFW Pythoneers, 6th
- Django Girls Abraka, 6-7th
- SciPy 2023 10-16th, Austin
- IndyPy, 11th
- Leipzig Python User Group, 11th
- Austin Python, 12th
- EuroPython 2023, 17-23rd
- Austin Python: Evening of Coding, 18th
- PyHEP.dev 2023 - "Python in HEP" Developer's Workshop, 25th
August 2023
- PyLadies Dublin, 15th
- EuroSciPy 2023, 14-18th
September 2023
- PyData Amsterdam, 14-16th
- PyCon UK, 22nd - 25th
🐍 Python project:
- Python
- Documentation
- News & Blog
- Python Planet blog aggregator
💓 Python Community:
- #python IRC for general questions
- #python-dev IRC for CPython developers
- PySlackers Slack channel
- Python Discord server
- Python Weekly newsletters
- Mailing lists
- Forum
✨ Python Ecosystem:
🌌 Fediverse
Communities
- #python on Mastodon
- c/django on programming.dev
- c/pythorhead on lemmy.dbzer0.com
Projects
- Pythörhead: a Python library for interacting with Lemmy
- Plemmy: a Python package for accessing the Lemmy API
- pylemmy pylemmy enables simple access to Lemmy's API with Python
- mastodon.py, a Python wrapper for the Mastodon API