Python 3.10+astterm
ast.MatchValue
pattern matching
A value pattern in a case: a literal or a dotted name (case 200, case Color.RED); compared with the subject using ==.
Syntax
ast.MatchValue(value)
The main match trap: a bare name is not a value. 'case RED:' yields not a MatchValue but a MatchAs — a capture that matches anything and rebinds RED; a name becomes a value only when dotted, 'case Color.RED:'. None, True and False are not MatchValue either but MatchSingleton, because they are compared with is.
Examples
import ast
src = 'match x:\n case 200: pass\n case Color.RED: pass\n case RED: pass'
cases = ast.parse(src).body[0].cases
print([type(c.pattern).__name__ for c in cases]) # → ['MatchValue', 'MatchValue', 'MatchAs']
See also
Open in the full glossarydocs.python.orgReport an inaccuracy