What are "soft keywords"?

PythonPython 3.9

Python Problem Overview


According to the documentation of the keyword module, two new members have been added in Python 3.9:

  • issoftkeyword
  • softkwlist

However their documentation doesn't reveal anything about their purpose. This change is not even mentioned in the What's New article, where typically all API changes are documented. Digging a little further into the source code eventually leads to this pull request where it is mentioned that "this is essentially an internal tool" and that "soft keywords are still unused". So what's the purpose of Python's soft keywords?

Python Solutions


Solution 1 - Python

Short: Soft keywords can still be used as variable or argument names.

PEP 622 sheds some light (Emphasis mine):

> The difference between hard and soft keywords is that hard keywords are always reserved words, even in positions where they make no sense (e.g. x = class + 1), while soft keywords only get a special meaning in context.

> [...] The match and case keywords are proposed to be soft keywords, so that they are recognized as keywords at the beginning of a match statement or case block respectively, but are allowed to be used in other places as variable or argument names.

Solution 2 - Python

I think this is best explained with a demo. async and await were soft keywords in Python 3.5 and 3.6, and thus they could be used as identifiers:

>>> async = "spam"
>>> async def foo():
...     pass
...
>>> await = "bar"
>>> async, await
('spam', 'bar')

But in Python 3.7 they became proper keywords and can only be used in specific contexts where they make sense:

>>> async = "123"
  File "<stdin>", line 1
    async = "123"
          ^
SyntaxError: invalid syntax
>>> async def foo():
...     pass
...
>>> await = "bar"
  File "<stdin>", line 1
    await = "bar"
          ^
SyntaxError: invalid syntax
>>> async, await
  File "<stdin>", line 1
    async, await
         ^
SyntaxError: invalid syntax

The idea in first introducing them as soft keywords was mainly to not break any existing code that uses them as identifiers. The same reasoning is with the upcoming match keyword which would completely break for example re.match and millions of projects.

Solution 3 - Python

Soft keywords are keywords that are context sensitive. For example, it'd allow you to use class as a variable name as long as it can't be interpreted as defining a class. It'd allow use to replace cls with class for example.

Today that's not possible, since class is a keyword:

>>> def a(class):
  File "<stdin>", line 1
    def a(class):
          ^

Given the context it's clear that the user didn't intend to define a new class, but wanted an identifier named class.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiona_guestView Question on Stackoverflow
Solution 1 - PythoncoukaView Answer on Stackoverflow
Solution 2 - PythonruoholaView Answer on Stackoverflow
Solution 3 - PythonMatsLindhView Answer on Stackoverflow