On Mon, Aug 30, 2021 at 11:32:20AM -0700, Nick Parlante wrote:
> Hi there python-ideas - I've been teaching Python as a first
> programming language for a few years, and from that experience I want
> to propose a change to PEP8. I'm sure the default position for PEP8 is
> to avoid changing it. However, for this one rule I think a good case
> can be made to make it optional, so let me know what you think.

I think you care too much about what PEP-8 says, and too little about 
why we use idiomatic language.

PEP-8 is not mandatory outside of the stdlib, and it is not intended to 
be best practice for teaching beginners, so why do you care so much 
about PEP-8? Just teach whatever standard you want. If you don't want to 
introduce `is` in your beginners course, that's fine.

But you should consider that good code is supposed to express the 
programmer's *intent*. The intent of using `obj is None` is to detect 
when the object `obj` is not just some value that happens to mimic 
None-ness by behaving the same, or some other object that compares equal 
to None, it actually *is* the None singleton.

(And None is always a singleton. That's a language guarantee.)

To put it in real world terms, I don't want a reproduction of the Mona 
Lisa, no matter how accurate it may be, I want the *actual* Mona Lisa.

It really doesn't matter that your students will probably never come 
across any sort of object that compares equal to None apart from None 
itself. The *intent* is what matters: using `obj is None` tells the 
reader clearly and idiomatically that you want the None singleton 
itself, and no other value or object no matter how closely it mimics 
None.

If by some unlikely chance or contrived circumstances your code has 
something which mimics None and compares equal to it, `if obj is None` 
says clearly that you *intend* to only accept the None singleton itself 
and no other value or object no matter how closely it mimics None.

As I said, PEP 8 is not mandatory, so teach whatever you like.

But `is` exists for a reason, and the idiom `obj is None` is not about 
slavishly following a style guide, or performance, or about the 
vanishingly small chance that you actually will come across something 
that compares equal to None. It is about sending a message to the 
reader that you want None and will accept no substitutes.


> To teach comparisons in Python, I simply say "just use ==" - it works 
> for ints, for strings, even for lists. Students are blown away by how 
> nice and simple this is. This is how things should work. Python really 
> gets this right.

Great. Go right ahead then. It's your course, you can teach whatever you 
like.

But if it were *my* course, rather than yours, I would teach a rule that 
is only a tiny bit more complex than "always use `==`":

Always use `==`, *except* when comparing to None, then always use `is`.

That's close enough for beginners. But as I said, if you want to only 
teach `==`, go right ahead.


-- 
Steve
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/LE7UMND47NUTGB4LT7MOMI6FEFLJEPOI/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to