soft keyword
In Python, a soft keyword is a special kind of keyword that retains its meaning only in specific contexts, while in other contexts, it behaves like a regular identifier. This means you can use soft keywords as variable names, function names, or other identifiers without causing syntax errors, except in the specific situations where Python interprets them as keywords.
Soft keywords allow for introducing new language features with minimal disruption to existing codebases, as they don’t reserve the word globally. So, they help Python evolve while maintaining backward compatibility.
Examples of soft keywords introduced in recent versions of Python include match
and case
, which are used in structural pattern matching feature introduced.
Example
Here’s an example illustrating how match
and case
act as soft keywords:
>>> # Used as identifiers
>>> match = "Singles"
>>> case = "Nominative"
>>> match
'Singles'
>>> case
'Nominative'
>>> # Used as soft keywords
>>> def http_status(status):
... match status:
... case 200:
... return "OK"
... case 404:
... return "Not Found"
... case _:
... return "Unknown"
...
>>> http_status(200)
'OK'
In this example, match
and case
serve dual purposes: they can act as regular variable names and, in the context of pattern matching, as keywords to define the matching logic.
Related Resources
Tutorial
Structural Pattern Matching in Python
In this tutorial, you'll learn how to harness the power of structural pattern matching in Python. You'll explore the new syntax, delve into various pattern types, and find appropriate applications for pattern matching, all while identifying common pitfalls.
For additional information on related topics, take a look at the following resources:
- Python Keywords: An Introduction (Tutorial)
- Using Structural Pattern Matching in Python (Course)
- Structural Pattern Matching (Quiz)
- Exploring Keywords in Python (Course)
- Python Keywords: An Introduction (Quiz)
By Leodanis Pozo Ramos • Updated May 22, 2025