Monthly Archives: August 2014

Some notes from PyGotham (corrections welcome)

Getting Rich with Comparison Methods 

Matt Story

the truth of x == y does not imply that x != y is false […] Mind == blown and mind != blown.

pro tip: when defining ‘__eq__’ also define ‘__ne__’ its reflection method

operators like ‘<‘ have reflection properties to handle ‘>’ when overridden
right-side reflection for left-side method

how much of life is wasted by not reading documentation first?

keep-calm-and-rtfm

python has a NotImplemented error. singleton constant

MRO –> method resolution order –> use the more specific class’s methods, regardless on which side of comparison
this is the reason to use mix-ins as opposed to normal inheritance

test ALL the things
import operator module to test all the cases for bitwise and arithmetic operators

eq, ne, lt, le, gt, ge – check for existence, return the thing or NotImplemented

@functools.total_ordering – may drastically reduce the code

comparison methods are well documented. fairly robust methodology to learn which method to use on right/left side. complex so need to test.

comparison methods do not need to return bools, so you can do whatever you want. harness asymmetrical and non-boolean comparisons… useful for lazy-loaded filtering, iteration

Continue reading