this post was submitted on 26 Jul 2023
12 points (100.0% liked)
Python
6405 readers
14 users here now
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
Feeds
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
This is pretty cool, but I have no idea what the significance is.
The key part is this:
Meaning, this is just a way to say that if you are defining some system that needs to conform with some interface, you can have type checked even if your have different objects from different classes. No need for TypeVar or define a crazy hierarchy: as long as the types implements the methods defined by the Protocol, the type checker will be happy.
It’s amazing how often people celebrate some new feature in a language and I’m like: TypeScript has been doing this for years now.
[Edit: This is also how interfaces work in Go, and it’s just the old advice “Code against interfaces, not classes.”]
It's almost like python and typescript are used by different people for different purposes and have therefor developed differently, but some features end up being an overall good idea which enrich their respective ecosystems.
You sound like an absolute tool.
[Edit: Who f*cking cares]
Whatever another programming language supports or does is entirely irrelevant if what you're working with is Python.
In practice, Protocols are a way to make "superclasses" that you can never add features to (for example,
readinto
despite being critical for performance is utterly broken in Python). This should normally be avoided at almost all costs, but for some reason people hate real base classes?If you really want to do something like the original article, where there's a C-implemented class that you can't change, you're best off using a (named)
Union
of two similar types, not aProtocol
.I suppose they are useful for operator overloading but that's about it. But I'm not sure if type checkers actually implement that properly anyway; overloading is really nasty in a dynamically-typed language.
Not really, and this interpretation is oblivious to the concept of protocols and misses the whole point of them.
The point of a protocol is to specify that a type supports a specific set of methods with specific signatures, aka duck typing, and provide the necessary and sufficient infrastructure to check if objects comply with a protocol and throw an error in case it doesn't.
Also, protocol classes can be inherited, and protocols can be extended.
https://peps.python.org/pep-0544/
Then - ignoring dunders that have weird rules - what, pray tell, is the point of protocols, other than backward compatibility with historical fragile ducks (at the cost of future backwards compatibility)? Why are people afraid of using real base classes?
The fact that it is possible to subclass a
Protocol
is useless since you can't enforce subclassing, which is necessary for maintainable software refactoring, unless it's a purely internal interface (in which case theUnion
approach is probably still better).That PEP link includes broken examples so it's really not worth much as a reference.
(for that matter, the
Sequence
interface is also broken in Python, in case you need another historical example of why protocols are a bad idea).Got both of those wrong. The point of protocols is to have a way to validate duck typing errors by adding a single definition of a duck. This is not something that only applies to backwards compatible, nor does it affects backwards compatibility.
You're missing the whole point of prototypes. The point of a prototype is that you want duck typing, not inheritance. Those are entirely different things, and with prototypes you only need to specify a single prototype and use it in a function definition, and it automatically validates each and any object passed to it without having to touch it's definition or add a base class.
That still doesn't explain why duck typing is ever a thing beyond "I'm too lazy to write
extends BaseClass
". There's simply no reason to want it.I already explained it to you: protocols apply to types you do not own or control, let alone can specify a base class.
You just specify a protocol, specify that a function requires it, and afterwards you can pass anything to it as-is and you'll be able to validate your calls.
and I already explained that
Union
is a thing.I'm not sure you understand that what a union does or does not do is completely irrelevant and besides the point. Python's protocols add support for structural subtyping, and enable both runtime and build-time type checks without requiring major code changes. Don't you understand what that means?