{"id":4506,"date":"2023-12-15T10:41:27","date_gmt":"2023-12-15T10:41:27","guid":{"rendered":"https:\/\/ibex.tech\/python\/?p=4506"},"modified":"2023-12-15T10:51:18","modified_gmt":"2023-12-15T10:51:18","slug":"typedef-enum-in-python","status":"publish","type":"post","link":"https:\/\/ibex.tech\/python\/memory-python\/typedef-enum-in-python","title":{"rendered":"typedef enum in Python"},"content":{"rendered":"\n<p>In C you might do this to create an automatically numbered from 0 list of values for example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>typedef enum _SM_USER_MODE\r\n{\r\n    UM_POWERUP,\r\n    UM_IDLE,\n    UM_DO_SOMETHING\r\n} SM_USER_MODE;<\/code><\/pre>\n\n\n\n<p>In python you can do the same it with the Enum class<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from enum import Enum, auto\n\n#Method 1\nclass UM(Enum):\r\n    UM_POWERUP = 0\r\n    UM_IDLE = auto()\r\n    UM_DO_SOMETHING = auto()\n\n#Method 2\nUM = Enum('UM', &#91;'UM_POWERUP', 'UM_IDLE', 'UM_DO_SOMETHING'], start=0)      #Use start=0 to stop Enum starting from 1<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Using Enum values<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>    print(list(UM))\n\n    print(UM.UM_POWERUP.value)\r\n    print(UM.UM_DO_SOMETHING.value)\r\n\n    #The value property can be omitted if the context doesn't require it, e.g.:\r\n    current_mode = UM.UM_IDLE\r\n    match (current_mode):\r\n        case UM.UM_POWERUP:\r\n            print(\"Do something A\")\r\n        case UM.UM_IDLE:\r\n            print(\"Do something B\")\r\r\n        case UM.UM_DO_SOMETHING:\r\n            print(\"Do something C\")\r\n        case _:\r\n            print(\"Unknown\")\r\n\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In C you might do this to create an automatically numbered from 0 list of values for example: In python you can do the same it with the Enum class Using Enum values<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[],"class_list":["post-4506","post","type-post","status-publish","format-standard","hentry","category-memory-python"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4506","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=4506"}],"version-history":[{"count":1,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4506\/revisions"}],"predecessor-version":[{"id":4509,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4506\/revisions\/4509"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/media?parent=4506"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/categories?post=4506"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/tags?post=4506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}