Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add note about dunder protocols that pop up in third-party libraries like numpy #44

Open
amontalenti opened this issue May 13, 2020 · 0 comments

Comments

@amontalenti
Copy link
Owner

In a chat, @ngoldbaum pointed out to me that this part isn't strictly true:

Never create your own names using __dunder__ adornments unless you are implementing a Python standard protocol, like __len__; this is a namespace specifically reserved for Python's internal protocols and shouldn't be co-opted for your own stuff.

Nathan explained that, for example, numpy defines several protocols that use dunder names (e.g. __array__), and that these protocols are widely used in the community.

I think I personally remember that sqlalchemy also uses a dunder protocol to link classes to SQL tables.

Some notes I wrote up in the chat:

Right, maybe I should say something like, "dunder protocols are primarily used by the Python core team, and occasionally have been used by very popular third-party modules (like numpy and sqlalchemy), but probably shouldn't be used in your code."
...
(My unexplained point in the guide is that whole reason that the Python team chose dunder names is to avoid reserving words in the grammar, while still having their own "private namespace" for class-level reserved words. So, it's a good idea to treat the entire __dunder__ namespace as a reserved namespace for the core team, since they could add new protocols at any time, as they did with context managers in 2.x, for example.)

@amontalenti amontalenti self-assigned this May 13, 2020
@amontalenti amontalenti changed the title Add note about __dunder__ protocols that pop up in third-party libraries like numpy Add note about dunder protocols that pop up in third-party libraries like numpy May 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant