Loading topic
match statement. At first glance it looks like the switch statement in C or Java. It does more than that. A switch compares one value against constants. A match statement compares a value against patterns, and a pattern can also pull the value apart and bind parts of it5 min readif/elif chains. It also assumes you know the difference between == (equal value) and is (same object). You have already seen the shape of a match statement. Here you will learn exactly what a literal pattern is4 min read_. It assumes you are on Python 3.10 or later and know functions, conditionals, lists, tuples, and * unpacking in assignments. It also assumes you have seen a basic match statement with literal case6 min readcase block handle several different patterns by joining them with |. This chapter covers where | can appear, how alternatives are tried, and the rules about bound names that produce syntax errors.5 min readcase. It assumes you know the earlier chapters on literal patterns, capture patterns and the _ wildcard. It also assumes Python 3.10 or later, and that you are comfortable with * unpacking in ordinary5 min readmatch and case blocks run, and that you have seen capture patterns, the _ wildcard, and sequence patterns. You should be comfortable with dictionaries and with ** unpacking.4 min readmatch and case syntax, capture patterns and the _ wildcard, and that you can read simple sequence, mapping and class patterns. It covers what a guard is, when it runs, which names it can see, and the mistakes that guards cause.5 min readmatch statements with literal, capture, sequence, mapping, and class patterns, and that you know how or-patterns (|) and guards work. You will learn how an as-pattern keeps a name for a value that a sub-pattern has already matched.5 min readmatch and case blocks work, what a capture pattern is, and how to define an Enum. It covers how to compare a subject against a named constant, which is where many first-time users of match get surprised.4 min readmatch statement with literal, capture, and wildcard patterns. It also assumes you can read dataclasses and know how attributes work. The goal is to match objects by type and by attribute values, and to know what Python does behind the scenes.5 min readPoint(x=0, y=0). It also assumes you know dataclasses and the difference between a capture name and a literal. You need Python 3.10 or later.4 min readmatch statement does its work. You should already know the basic match and case syntax, capture and wildcard patterns, and guards. You need Python 3.10 or later.6 min readmatch bugs come from three sources. A bare name in a pattern captures instead of comparing. An earlier case hides a later one. Captured values and guards touch state you did not expect. This chapter shows each failure and how to avoid it.6 min read