4 ms·
This was my work (not sure why someone randomly posted it here, but such is the internet). I do exactly what you just described, see http://www.rafekettler.com
by rafekett 15y ago
This was my work (not sure why someone randomly posted it here, but such is the internet).
I do exactly what you just described, see http://www.rafekettler.com/magicmethods.html#appendix http://www.rafekettler.com/magicmethods.html#appendix
- cskau 15y agoSuch is the Internet indeed. I stumbled upon this while researching some Python magic. I felt it was a pretty good write-up so I decided to share it with my fellow HN'ers. Thank you for posting this.
- rafekett 15y agoAnd thank you for posting. I'll take all of the traffic I can get :)
- ceol 15y agoI'm quite glad someone posted it here! I'm not sure if I would have found such a useful reference otherwise. Thanks for writing this up!
- masklinn 15y agoYou should not document __coerce__, it shouldn't be used anymore. Also why put __nonzero__, __str__ and __unicode__ in "Representing your Classes" instead of "Type conversion magic methods"? Why not talk about the collections ABCs for container operators? (using them as mixins greatly implifies implementing containers, and makes it clear what's required and what is not)? And the __concat__ note is incorrect, that does not exist in the userland datamodel it exists only in the `operator` module and in the C sequence protocol.
- jerf 15y agoThe word example is dangerous; two different words of the same length are neither less than each other, greater than each other, or equal to each other. I think sorting may not work as well as you'd like, and you should just be passing a custom cmp function to the sort method. The other comparison methods are for optimization; they should never disagree with __cmp__, and if __cmp__ is already optimal you should usually not define them.