{"id":4508,"date":"2023-12-15T10:45:55","date_gmt":"2023-12-15T10:45:55","guid":{"rendered":"https:\/\/ibex.tech\/python\/?p=4508"},"modified":"2024-11-26T17:34:27","modified_gmt":"2024-11-26T17:34:27","slug":"match","status":"publish","type":"post","link":"https:\/\/ibex.tech\/python\/match-switch\/match","title":{"rendered":"match"},"content":{"rendered":"\n<p>In python the equivalent to switch case in other languages is match:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    current_mode = 2\n    match (current_mode):\n        case 1:\n            print(\"Do something A\")\n        case 2:\n            print(\"Do something B\")\n        case 3:\n            print(\"Do something C\")\n        case _:         #&lt;&lt;&lt;&lt;This is the equivalent of \"default:\" (_ is a wildcard)\n            print(\"Unknown\")\n\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">break<\/h4>\n\n\n\n<p>There is no break in Python for match. break is only used for loops.<\/p>\n\n\n\n<p>If you want to achieve being able to break from a match case early you can use an exception trick like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    current_mode = 2\n    try:\n        match (current_mode):\n            case 1:\n                print(\"Do something A\")\n            case 2:\n                print(\"Do something B1\")\n                raise GeneratorExit(\"\")    #We can use a specific error type like 'GeneratorExit' so we can trap it seperatly from actual errors we may want to handle\n                print(\"Do something B2\")\n            case 3:\n                print(\"Do something C\")\n            case _:\n                print(\"Unknown\")\n    except GeneratorExit:\n        #match break equivalent exit point (use raise GeneratorExit(\"\") )\n        pass    #placeholder code needed for the except\n\n    print(\"We're out of the match\")<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In python the equivalent to switch case in other languages is match: break There is no break in Python for match. break is only used for loops. If you want to achieve being able to break from a match case early you can use an exception trick like this:<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[361],"tags":[],"class_list":["post-4508","post","type-post","status-publish","format-standard","hentry","category-match-switch"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4508","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/comments?post=4508"}],"version-history":[{"count":8,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4508\/revisions"}],"predecessor-version":[{"id":4771,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4508\/revisions\/4771"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/media?parent=4508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/categories?post=4508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/tags?post=4508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}