> Keep in mind that you're not meant to invent your own dunder methods. Sometimes you'll see third-party libraries that do invent their own dunder method, but this isn't encouraged and it can be quite confusing for users who run across such methods and assume they're "real" dunder methods.I don't think this is good advice. Example: the dunder methods implemented and used by the Scientific Python/PyData community (__array__, __array_ufunc__, __array_func__, etc.) are so, so, so important to that ecosystem.
ayhanfuat|1 year ago
> System-defined names, informally known as “dunder” names. These names are defined by the interpreter and its implementation (including the standard library). Current system names are discussed in the Special method names section and elsewhere. More will likely be defined in future versions of Python. Any use of __*__ names, in any context, that does not follow explicitly documented use, is subject to breakage without warning.
ddavis|1 year ago
jameshart|1 year ago
Such declarations MUST be preceded by the comment:
darkflame91|1 year ago
kodablah|1 year ago
enragedcacti|1 year ago
https://peps.python.org/pep-0008/#descriptive-naming-styles
shadowgovt|1 year ago
But in general, Joe Random Developer's library is not that important to justify nibbling away at a function namespace that has no mechanism for keeping two libraries from choosing the same name with different semantic meaning.
jonathrg|1 year ago
jimbokun|1 year ago
If not, dunder methods do not seem appropriate.
anamexis|1 year ago
akvadrako|1 year ago
hobs|1 year ago
TomBombadildoze|1 year ago
https://docs.python.org/3/reference/lexical_analysis.html#re...
This has been in the specification since, at latest, Python 2.0, and perhaps earlier.
Any you see defined outside the Python data model are entirely historical, written in contradiction to the spec. Many of them also can't be changed because there is so much code in the wild that depends on them.
*Do not define your own dunder methods!*
agency|1 year ago
[1] https://data-apis.org/dataframe-protocol/latest/