![]() One important feature of pattern matching is that at most one pattern will be matched. Line 10 matches any dictionary with a "dob" key. Line 8 matches a dictionary with a "dob" key whose value is another dictionary with an integer ( int) item named "age". Each case describes one pattern, and the indented block beneath it says what should happen if there’s a match. One or several case statements follow match. In this example, that’s the user data structure. ![]() You start with a match statement that specifies what you want to match. case construct is new in Python 3.10 and is how you perform structural pattern matching. case block to find the first name of a user by extracting it from a user data structure: Time to match your first pattern! The following example uses a match. The next section will step back and explain the patterns in more detail. Instead, it will try to give you an impression of the possibilities. This section will be a bit light on explanations of the possible patterns. You’ll define several patterns, and depending on which pattern matches your data, you’ll process your data appropriately. In this section, you’ll study a practical example where you’ll work with data that are structured differently, even though the meaning is the same. Deconstructing Data StructuresĪt its core, structural pattern matching is about defining patterns to which your data structures can be matched. You’ll also see some links that can help you explore in even more depth if you want. To give you a taste of how you can take advantage of it in your own projects, the next three subsections will dive into some of the details. Structural pattern matching is a comprehensive addition to the Python language. Detecting and deconstructing different structures in your data.You’ll see three different examples that together highlight why this feature is called structural pattern matching and show you how you can use this new feature: case coming to Python, but you’ll see that structural pattern matching is much more powerful than that. Its introduction has sometimes been referred to as switch. The biggest new feature in Python 3.10, probably both in terms of controversy and potential impact, is structural pattern matching. Alternatively, you can use Docker with the latest Python image. To try out the new features yourself, you need to run Python 3.10. ![]() Checking the length of sequences when using zip().Adding more readable and more specific type hints.Using structural pattern matching to work with data structures.Debugging with more helpful and precise error messages.Here, you’ll get to learn about the coolest new features. You can read about all of them in the documentation. As of October 4, 2021, the first official version is available.Įach new version of Python brings a host of changes. Python 3.10 is out! Volunteers have been working on the new version since May 2020 to bring you a better, faster, and more secure Python. Watch it together with the written tutorial to deepen your understanding: Cool New Features in Python 3.10 Watch Now This tutorial has a related video course created by the Real Python team. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |