
On 2021-08-30 12:34, Nick Parlante wrote:
Like for "if x == None:" works right
By that definition, == is correct there. I think PEP8 has forced people to use "is" so much, they've lost touch with the reality that in fact == would work perfectly in almost all cases. Situations where "is" is required for correctness are very rare.
But you can't know if `x == None` is correct without knowing what `x` is. For instance, as someone mentioned earlier in this thread ,if `x` is a numpy array, `x == None` will return another numpy array, and trying to use that in an `if` will give an error. The basic issue is that objects can customize what `==` does, but they can't customize what `is` does. That means that `is` is "safer" in the sense that you always know it's going to do what it always does, regardless of the types of the objects you're comparing. I agree with Chris though that it sounds like your issue is really one of teaching rather than one of PEP8. It's fine to not teach students how to do everything according to PEP8 right from the get-go. Especially for something like `is None`, it's unlikely to be a huge deal for students to later learn the "correct" (aka PEP8-endorsed) way, once they have a more nuanced understanding of how comparisons work in Python. There are all kinds of recommendations in PEP8 that aren't worth worrying about at the initial stages of learning Python. -- Brendan Barnwell "Do not follow where the path may lead. Go, instead, where there is no path, and leave a trail." --author unknown