Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Understanding Advanced Patterns

00:00 Python also supports more advanced structural patterns. These include sequence patterns, mapping patterns, class patterns, and AS patterns.

00:11 Sequence patterns are a way to match a subject by its shape. Sequence patterns are comma-separated items optionally wrapped in square brackets or round brackets.

00:20 They match a subject by its shape using destructuring. They can use other patterns as elements like literal or capture patterns. In this example, a capture pattern is used. match subject case [1, 2, 3], binding each variable to an element of the sequence.

00:37 Print an f-string: its and the contents of one, two, and three. This case clause will match and print any sequence with three elements.

00:48 Most iterable Python sequences can be matched this way,

00:52 including tuple(), list(), range(), memoryview(), array.array(), collections.deque() objects, collections. namedtuple() objects, as well as any user-defined classes inheriting from Sequence or MutableSequence.

01:09 However, the Python sequence types, str, bytes and bytearray will not be supported as matchable sequences.

01:19 Next, we have mapping patterns. You can match by the key-value structure of an object as well. Mapping patterns work with Python’s key-value mappings like dictionaries. They match a subject by its shape using destructuring, just like sequence patterns.

01:32 They require that the keys used in the patterns be literal or value patterns, and they’ll ignore keys not included in the pattern.

01:40 For example, match subject case and then what looks like a dictionary literal with the key, the string, “the_key”, and the value, the string, “Python”, and then print “It’s Python”.

01:53 This matches to any mapping with the key-value pair, the key, and Python.

02:00 Finally, we have class and AS patterns. These two patterns can be combined to build highly specific patterns. Class patterns use class constructors to facilitate pattern matching by type. Class constructors can be built-ins like int or float or any other user-defined classes. AS patterns allow you to bind a matched sub-pattern to a variable.

02:22 This is especially useful for cases where a capture pattern isn’t feasible, but you still want to access the matched sub-pattern within the case block.

02:30 This familiar example comes from early on in the course, and now you can understand how the whole thing works. match subject case list, which enforces the list type and a sequence pattern of int or float as x, int or float as y, 0 and print an f-string that contains the word “Point”, and reports the x and y values retrieved from the AS patterns above.

02:54 This pattern will match a list of two int or float objects using class patterns, and a third element equal to zero, also binding the first two items to x and y. Class patterns like this are especially useful for data validation at runtime. Open your IDE and we’ll code up an example.

Become a Member to join the conversation.