Skip to content

Conversation

@JohnADawson
Copy link

@JohnADawson JohnADawson commented Aug 14, 2024

Since version 3.10 match has been a soft keyword, so examples ought to not use
it as a variable name.

https://docs.python.org/3/reference/lexical_analysis.html#soft-keywords

The new name is m, that used for match objects in the other examples in
re.rst.


📚 Documentation preview 📚: https://cpython-previews--123003.org.readthedocs.build/

Since version 3.10 `match` has been a soft keyword, so examples ought to not use
it as a variable name.

https://docs.python.org/3/reference/lexical_analysis.html#soft-keywords

The new name is `m`, that used for match objects in the other examples in
re.rst.
@ghost
Copy link

ghost commented Aug 14, 2024

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-app bedevere-app bot added docs Documentation in the Doc dir skip news awaiting review labels Aug 14, 2024
@picnixz
Copy link
Member

picnixz commented Aug 14, 2024

I would say it's also for consistency with the rest of the module where we use m = (...).match.

Nevermind, you already observed it.

Comment on lines +1360 to +1362
m = re.search(pattern, string)
if m:
process(m)
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)

).

Match objects always have a boolean value of ``True``.
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

4 participants