From: Bruno Haible <bruno@clisp.org>
To: bug-gnulib@gnu.org
Cc: Dima Pasechnik <dimpase@cs.ox.ac.uk>,
Collin Funk <collin.funk1@gmail.com>
Subject: Re: Python != None
Date: Sun, 25 Feb 2024 12:57:15 +0100 [thread overview]
Message-ID: <41613867.J2Yia2DhmK@nimes> (raw)
In-Reply-To: <005ECEB2-5D0D-43C6-8AA2-B44D6AF0219E@cs.ox.ac.uk>
Collin Funk wrote:
> One thing that annoys me personally
> is comparing to none using "!=" instead of "is not". This is
> recommended against in PEP 8, "Comparisons to singletons like None
> should always be done with is or is not, never the equality
> operators." [1].
Dima Pasechnik wrote:
> The pythonic way is
>
> mode is not None
>
> rather than
>
> mode != None
>
> (the reason is None is an object)
Summarizing the arguments for "is not None":
1- Clarity and readability, best practice, PEP 8.
2- Identity comparison vs. value comparison.
3- None is a singleton.
4- Prevents wrong results if an __eq__ method has been incorrectly coded.
Here's my take on it.
1- is clearly subjective. Clarity is context dependent. Etc.
2- Yes, https://docs.python.org/3.12/reference/expressions.html#comparisons
clearly explains the difference between == and 'is'.
(It's more or less like the difference between EQUAL and EQ in Lisp.)
3- None is a singleton, but it nonetheless supports both == and 'is'. So,
this is a weak argument.
4- This is one of the weakest possible arguments.
The style warnings about "!= None" in pycodestyle and/or pylint are
relativized by this warning in Python itself:
>>> '' != None
True
>>> '' is not None
<stdin>:1: SyntaxWarning: "is not" with a literal. Did you mean "!="?
True
So, if Python itself warns about some code that follows PEP 8...
it means that we need to think carefully, rather than blindly apply
that PEP 8 rule.
I think a better rule, in particular in the context of gnulib-tool.py,
is to observe that each variable has a certain set of possible values
(even though this set of values is not explicitly stated in the source
code), and with this set of possible values comes a comparison operator.
So, what I mean is:
* If a variable can only contain objects like GLModule instances or None,
then 'is' (pointer comparison) is the right choice for this variable.
* If a variable can only contain strings or None, then '==' (value
comparison) is the right choice for this variable, since comparing
strings with 'is' is unreliable:
>>> x = "ab"
>>> y = "ab"
>>> x is y
True
>>> x = "abc"[:2]
>>> y = "abx"[:2]
>>> x is y
False
* If a variable can only contain a list or None, then '==' (value comparison)
is the right choice for this variable.
>>> ["a"] == ["a"]
True
>>> ["a"] is ["a"]
False
So, depending on the variable:
mode != None OK
modules != None OK
module != None not OK, better write: module is not None
Bruno
next prev parent reply other threads:[~2024-02-25 11:57 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-23 5:23 [PATCH] gnulib-tool.py: Follow gnulib-tool changes, part 27 Collin Funk
2024-02-23 13:08 ` Bruno Haible
2024-02-23 22:20 ` Collin Funk
2024-02-23 23:51 ` Bruno Haible
2024-02-24 2:36 ` Collin Funk
2024-02-24 5:49 ` gnulib-tool.py: Follow gnulib-tool changes, part 28 Collin Funk
2024-02-24 23:25 ` gnulib-tool.py: Follow gnulib-tool changes, part 27 Bruno Haible
2024-02-25 0:03 ` Dima Pasechnik
2024-02-25 11:57 ` Bruno Haible [this message]
2024-02-25 19:29 ` Python != None Collin Funk
2024-02-25 20:07 ` Collin Funk
2024-02-26 20:38 ` pycodestyle configuration Bruno Haible
2024-02-26 21:31 ` Collin Funk
2024-02-26 22:54 ` Bruno Haible
2024-02-27 0:51 ` Collin Funk
2024-02-27 2:38 ` Bruno Haible
2024-02-27 4:22 ` Collin Funk
2024-02-25 20:55 ` Python != None Dima Pasechnik
2024-02-25 12:02 ` Python 'strings' Bruno Haible
2024-02-25 19:05 ` Collin Funk
2024-02-24 23:42 ` gnulib-tool.py: Follow gnulib-tool changes, part 28 Bruno Haible
2024-02-25 0:47 ` Collin Funk
2024-02-25 1:18 ` Collin Funk
2024-02-25 1:25 ` Bruno Haible
2024-02-25 3:32 ` Collin Funk
2024-02-26 20:51 ` Bruno Haible
2024-02-28 11:51 ` Collin Funk
2024-02-28 12:14 ` Bruno Haible
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=41613867.J2Yia2DhmK@nimes \
--to=bruno@clisp.org \
--cc=bug-gnulib@gnu.org \
--cc=collin.funk1@gmail.com \
--cc=dimpase@cs.ox.ac.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).