4

I don't fully understand why the keyword match can be used as a variable or function name, unlike other keywords if, while, etc.?

>>> match "abc":
...     case "abc":
...         print('Hello!')
...     
Hello!
>>> from re import match
>>> match('A', 'A Something A')
<re.Match object; span=(0, 1), match='A'>
>>> match = '????'
>>> match
'????'
>>> case = 'something'
>>> case
'something'
Be3y4uu_K0T
  • 318
  • 4
  • 10
  • 2
    Have you read [PEP 622](https://www.python.org/dev/peps/pep-0622/#backwards-compatibility)'s section on backwards compatibility? – Brian61354270 Aug 11 '21 at 15:00
  • 1
    [Please do not upload images of code/errors when asking a question.](//meta.stackoverflow.com/q/285551) Include it as a [formatted code block](//stackoverflow.com/help/formatting) instead of an image. [Why do we hate screenshots so much?](//meta.stackoverflow.com/q/303812/) – Pranav Hosangadi Aug 11 '21 at 15:02
  • 1
    Think about this: how much existing code would break if it couldn't? – jonrsharpe Aug 11 '21 at 15:08

1 Answers1

8

Per PEP 622, match and case are being added as "soft keywords", so they will remain valid identifiers:

This PEP is fully backwards compatible: the match and case keywords are proposed to be (and stay!) soft keywords, so their use as variable, function, class, module or attribute names is not impeded at all.

Brian61354270
  • 8,690
  • 4
  • 21
  • 43