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

Using Structural Pattern Matching in Python (Summary)

Congratulations! You’ve now acquired a thorough understanding of structural pattern matching in Python. By mastering the match statement and case clauses, you’ve unlocked powerful tools for writing more concise and readable code.

Along the way, you explored a wide variety of pattern types, from simple literals and value patterns to more complex sequences, mappings, and class patterns. You’ve also learned how to customize pattern matching for user-defined classes so you can extend this powerful feature to your own types.

In this video course, you’ve:

  • Mastered the syntax of the match statement and case clauses
  • Explored various types of patterns supported by Python
  • Learned about guards, unions, aliases, and name binding
  • Extracted values from deeply nested hierarchical data structures
  • Customized pattern matching for user-defined classes
  • Identified and avoided common pitfalls in Python’s pattern matching

With this knowledge in your toolkit, you can now leverage structural pattern matching to make your Python code more declarative and expressive. Whether you’re validating data, implementing control flow, or just aiming to write cleaner, more maintainable code, pattern matching can be a powerful ally.

Download

Course Slides (.pdf)

4.3 MB
Download

Sample Code (.zip)

4.1 KB
Avatar image for bhchurch6

bhchurch6 on March 19, 2025

I attemped to execute the curl command from the last few lessons using my Mac terminal. I dropped the ‘jq’ portion of the command since I did not have it installed. I ran the command as:

$ curl -s 'https://api.github.com/repos/python/cypthon/events' | head -n 48

What I received back was the following:

{
  "message": "Not Found",
  "documentation_url": "https://docs.github.com/rest/activity/events#list-repository-events",
  "status": "404"
}

I tried accessing the link via my browser and got the same result. Based upon the documentation I received when I just executed the command:

$ curl -s 'https://api.github.com/' | head -n 48'

I think I copied the URL correctly. Is there something wrong in the command that I missed? There is a URL in the response from the command that is ‘api.github.com/events’. However it does not specify an {owner} or {repos}.

Thank you, Bruce

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on March 20, 2025

@bhchurch6 When you follow the provided link with the GitHub API documentation, you’ll find that the /events endpoint requires authentication:

$ curl -L \
  -H "Accept: application/vnd.github+json" \
  -H "Authorization: Bearer <YOUR-TOKEN>" \
  -H "X-GitHub-Api-Version: 2022-11-28" \
  https://api.github.com/repos/OWNER/REPO/events

You can generate a fine-grained personal access token required by GitHub here: github.com/settings/personal-access-tokens/new

In the next step, you’ll need to copy your new token and store it safely, as GitHub will never show it to you again!

Once you have your token, you can use it along with the curl command. For example, here’s mine:

$ curl -L \
  -H "Accept: application/vnd.github+json" \
  -H "Authorization: Bearer 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" \
  -H "X-GitHub-Api-Version: 2022-11-28" \
  https://api.github.com/repos/python/cpython/events
[
  {
    "id": "47760096152",
    "type": "WatchEvent",
    "actor": {
      "id": 149587415,
      "login": "Ndgt",
      "display_login": "Ndgt",
      "gravatar_id": "",
      "url": "https://api.github.com/users/Ndgt",
      "avatar_url": "https://avatars.githubusercontent.com/u/149587415?"
    },
    "repo": {
      "id": 81598961,
      "name": "python/cpython",
      "url": "https://api.github.com/repos/python/cpython"
    },
    "payload": {
      "action": "started"
    },
    "public": true,
    "created_at": "2025-03-20T04:24:35Z",
    "org": {
      "id": 1525981,
      "login": "python",
      "gravatar_id": "",
      "url": "https://api.github.com/orgs/python",
      "avatar_url": "https://avatars.githubusercontent.com/u/1525981?"
    }
  },
  (...)
]

Now, you’re getting the JSON output you were looking for.

Become a Member to join the conversation.