Python Glossary Русский

Python 3.10+astterm

ast.MatchSingleton

pattern matching

The pattern case None, case True or case False: value holds the object itself, and matching uses is rather than ==.

Syntax

ast.MatchSingleton(value)

Because of is, 'case True:' does not match 1 even though 1 == True; likewise 'case False:' misses 0, whereas 'case 1:' (a MatchValue) matches True as well. The value field holds a plain Python object, not a Constant node — unlike MatchValue, where value is an expression.

Examples

import ast
p = ast.parse('match x:\n    case None: pass').body[0].cases[0].pattern
print(type(p).__name__, p.value)  # → MatchSingleton None
def kind(v):
    match v:
        case True:
            return 'True'
        case 1:
            return 'one'
    return 'other'
print(kind(1), kind(True))  # → one True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy