

Python FAQ: Equality - easonchan42
http://me.veekun.com/blog/2012/03/24/python-faq-equality/

======
pixelmonkey
OP has: "did you call SomeClass() twice? Then a is b will always be False."

Not quite. SomeClass can have an implementation of __new__ which returns the
same object upon every invocation. In this case "SomeClass() is SomeClass()"
will be True. People sometimes do this and call SomeClass a "singleton".

~~~
bendotc
Yeah, I wasn't crazy about the implication that str and int only do this via
special built-in magic. In reality, you can overload __new__ to return
whatever you want, including object interning.

------
shawnps
In the first example under "When to use which", the reason you want to set a
default argument in a function to None, check if it is None, and then set it
to an empty list is explained here:

<http://effbot.org/zone/default-values.htm>

In short, it can be bad to set default arguments to mutable objects because
the function keeps using the same object in each call.

------
rflynn
Good stuff. I was playing around with ordinality of language built-ins
recently and found unintuitive results for Python
(<https://github.com/rflynn/wat/blob/master/src/py.ord.png>). As your article
mentions there is a fine line between language and implementation detail.

    
    
        >>> u"" > () > "" == u""
        True

~~~
j_baker
<http://docs.python.org/library/stdtypes.html#comparisons>

CPython implementation detail: Objects of different types except numbers are
ordered by their type names; objects of the same types that don’t support
proper comparison are ordered by their address.

------
gbog
> Use arg is None when you have a function with an argument defaulting to
> None. That’s okay, because there’s only oneNone.

In most real cases you want to get out if arg is either None offer any false
value, so maybe "if not arg" ?

> For testing whether two classes, functions, or modules are the same object,
> is is okay. Stylistic choice.

If "is" is not better than = I would advise against it in all cases where it
is not necessary: always use = except when you do tricky things. The argument
that = can be overridden is wrong: if it really is the case, just use another
library or fix it. You should nor write defensive code at this level.

------
technomancy
Equality done right: <http://home.pipeline.com/~hbaker1/ObjectIdentity.html>

~~~
ced
I like Kent Pitman's explanation of why there cannot be One Universal Equality
in Lisp:

<http://www.nhplace.com/kent/PS/EQUAL.html>

~~~
technomancy
It's worth noting that the problems he mentions are specific to Common Lisp.
Other lisps make it possible to implement egal.

