You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.)
The text was updated successfully, but these errors were encountered:
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
In a chat, @ngoldbaum pointed out to me that this part isn't strictly true:
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:
The text was updated successfully, but these errors were encountered: