Actually keywords are predefined, reserved names in Python that have special meaning for language's parser. Sometime they declare that we're about to define:
- An instance of a type (like what
def
and class
keywords does)
- Condition statement (like
if
, while
...)
- ...
- Some times they are actual objects like
True
, False
, None
.
Because I see no body mentioned, we have two types of keywords: (Examples are from python 3.10)
1- Hard keywords: (keyword.kwlist
)
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break',
'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally',
'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal',
'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
2- Soft keywords: (keyword.softkwlist
)
['_', 'case', 'match']
You can not use hard keywords as a variable name or assign something to them. They are reserved names in all places. But you can have a variable called match
and also you can have them inside an expression as a normal variable. match
is only have special meaning if it is in the first line of a match-case block.
From PEP 634:
Remember that match and case are soft keywords, i.e. they are not
reserved words in other grammatical contexts (including at the start
of a line if there is no colon where expected).