On Tue, Aug 31, 2021 at 06:18:15PM -0400, Todd wrote:
You insist that both approaches should be treated as equally valid. But they simply aren't. In the real world, where people are trying to do almost anything useful with python, approach 2 is too dangerous to rely on.
In fairness to Nick, he is not talking about the real world. Nick is talking about the hot-house environment of *education*, where fragile newbies are generally protected from real world issues. And let's not exaggerate the issue either by using emotive words like "dangerous". Outside of numpy arrays and pandas dataframes, I think it is highly unlikely that anyone in the real world will come across cases where `x == None` will cause your program to silently do the wrong thing in a way that can't be solved by telling them "well don't do that then". In almost all cases, the test we are looking at is used to decide whether or not to replace a parameter with a mutable default. So in practical terms, the worst thing that will happen is that the caller gets the default when they didn't want it. Numpy arrays and pandas dataframes are two big, important and common exceptions, but they are exceptional for a different reason: they don't even return True or False. Outside of contrived counter-examples, we're not likely to come across anything trying to mimac None in the real world. But that's not really why we use `is`, or at least, it's not the only reason. There are a bunch of reasons, none of which on their own are definitive, but together settle the issue (in my opinion). 1. Avoid rare bugs caused by weird objects. 2. Slightly faster and more efficient. 3. Expresses the programmer's intent. 4. Common idiom, so the reader doesn't have to think about it. -- Steve