Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Doc/library/re.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1357,9 +1357,9 @@ Since :meth:`~Pattern.match` and :meth:`~Pattern.search` return ``None``
when there is no match, you can test whether there was a match with a simple
``if`` statement::
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
``if`` statement::
``if ... is not None`` statement::

I agree with Adam that the example should be updated, but the text should be updated accordingly.


match = re.search(pattern, string)
if match:
process(match)
m = re.search(pattern, string)
if m:
process(m)
Comment on lines +1379 to +1381
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To promote good practice, we could suggest

Suggested change
m = re.search(pattern, string)
if m:
process(m)
m = re.search(pattern, string)
if m is not None:
process(m)

If we want to suggest using assignment expressions, we could also use:

Suggested change
m = re.search(pattern, string)
if m:
process(m)
if m := re.search(pattern, string):
process(m)

Or both! (I probably wouldn't go for this...)

Suggested change
m = re.search(pattern, string)
if m:
process(m)
if (m := re.search(pattern, string)) is not None:
process(m)

A

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having is not None does not seem to me better practice. There are two possibly relevant parts of PEP 8:

Also, beware of writing if x when you really mean if x is not None – e.g. when testing whether a variable or argument that defaults to None was set to some other value. The other value might have a type (such as a container) that could be false in a boolean context!

and

For sequences, (strings, lists, tuples), use the fact that empty sequences are false:

# Correct:
if not seq:
if seq:
# Wrong:
if len(seq):
if not len(seq):

Now, the first does not apply here, because, as the documentation immediately before the example says,

Match objects always have a boolean value of True. Since match() and search() return None when there is no match, you can test whether there was a match with a simple if statement [Emphasis added]

On the other hand, the spirit of the second excerpt from PEP 8 argues for availing of truthiness and falsiness and thus against the redundant is not None.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not have a strong opinion on whether to use the assignment operator here. (I myself would not, reserving it for cases like

[process(m) for pattern in patterns if (m := re.search(pattern, string)]

and

if ...:
    ...
elif (m := re.search(pattern, string):
    process(m)

).


.. class:: Match

Expand Down