Why/When in Python does `x==y` call `y.__eq__(x)`?

You’re missing a key exception to the usual behaviour: when the right-hand operand is an instance of a subclass of the class of the left-hand operand, the special method for the right-hand operand is called first. See the documentation at: http://docs.python.org/reference/datamodel.html#coercion-rules and in particular, the following two paragraphs: For objects x and y, first x.__op__(y) … Read more

(obj == null) vs (null == obj)?

You can’t accidently assign null to obj by typing obj = null instead. However, that’s a reminiscence from C times, in java, it is not possible, as the = expression returns the right hand side of the assignment. As null is not a boolean, the compiler will complain. I would try to explain it to … Read more

String comparison – strA.ToLower()==strB.ToLower() or strA.Equals(strB,StringComparisonType)?

You haven’t specified a platform, but I’m guessing .NET. I would strongly suggest you use the latter form – because case comparisons aren’t as simple as you might expect. (It also avoids creating extra strings, but that’s a different matter.) For example, what do you want your code to do when presented with “mail” and … Read more