{"id":134766,"date":"2023-09-14T20:48:40","date_gmt":"2023-09-14T20:48:40","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=1651559"},"modified":"2023-09-14T20:48:40","modified_gmt":"2023-09-14T20:48:40","slug":"python-enum-get-value-five-best-methods","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2023\/09\/14\/python-enum-get-value-five-best-methods\/","title":{"rendered":"Python Enum Get Value \u2013 Five Best Methods"},"content":{"rendered":"\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-top\" data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;1651559&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;4&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;4\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;Python Enum Get Value - Five Best Methods&quot;,&quot;width&quot;:&quot;113.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 113.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 4\/5 &#8211; (1 vote) <\/div>\n<\/p><\/div>\n<p>This article delves into the diverse methods of extracting values from Python&#8217;s <code>Enum<\/code> class. The <code>Enum<\/code> class in Python offers a platform to define named constants, known as enumerations. <\/p>\n<p>These enumerations can be accessed through various techniques:<\/p>\n<ol class=\"has-global-color-8-background-color has-background\">\n<li><strong>By Name\/Key:<\/strong> Access the enumeration directly through its designated name or key.<\/li>\n<li><strong>By String\/String Name:<\/strong> Utilize a string representation of the enumeration&#8217;s name, often in conjunction with the <code>getattr<\/code> function.<\/li>\n<li><strong>By Index:<\/strong> Retrieve the enumeration based on its sequential order within the <code>Enum<\/code> class.<\/li>\n<li><strong>By Variable:<\/strong> Leverage a variable containing the enumeration&#8217;s name, typically paired with the <code>getattr<\/code> function.<\/li>\n<li><strong>By Default:<\/strong> Obtain the initial or default value of the enumeration, essentially the foremost member defined in the <code>Enum<\/code> class.<\/li>\n<\/ol>\n<p>This article underscores the adaptability and multifaceted nature of the <code>Enum<\/code> class in Python, illustrating the myriad ways one can access the values of its constituents.<\/p>\n<h2 class=\"wp-block-heading\">Method 1: Python Enum Get Value by Name<\/h2>\n<p><strong>Problem Formulation: <\/strong>How can you retrieve the value of an Enum member in Python using its name or key?<\/p>\n<p class=\"has-global-color-8-background-color has-background\">In Python, the <code>Enum<\/code> class allows you to define named enumerations. To get the value of an Enum member using its name (=key), you can directly access it as an attribute of the Enum class.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" fetchpriority=\"high\" width=\"433\" height=\"128\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/image-78.png\" alt=\"\" class=\"wp-image-1651561\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/image-78.png 433w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/image-78-300x89.png 300w\" sizes=\"(max-width: 433px) 100vw, 433px\" \/><\/figure>\n<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from enum import Enum\nclass Color(Enum): RED = 1 GREEN = 2 BLUE = 3\nprint(Color.RED.value)\n# 1<\/pre>\n<\/p>\n<h2 class=\"wp-block-heading\">Method 2: Python Enum Get Value by String\/String Name<\/h2>\n<p><strong>Problem Formulation: <\/strong>How can you retrieve the value of an Enum member in Python using a string representation of its name?<\/p>\n<p class=\"has-global-color-8-background-color has-background\">You can use the string representation of an Enum member&#8217;s name to access its value by employing the <code><a href=\"https:\/\/blog.finxter.com\/python-getattr\/\">getattr()<\/a><\/code> function.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">color_name = \"RED\"\nprint(getattr(Color, color_name).value)\n<\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/blog.finxter.com\/python-getattr\/\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/getattr-1-scaled-1.jpg\" alt=\"\" class=\"wp-image-1651562\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/getattr-1-scaled-1.jpg 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/getattr-1-scaled-1-300x169.jpg 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/getattr-1-scaled-1-768x432.jpg 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n<h2 class=\"wp-block-heading\">Method 3: Python Enum Get Value by Index<\/h2>\n<p><strong>Problem Formulation: <\/strong>How can you retrieve the value of an Enum member in Python using its index?<\/p>\n<p class=\"has-global-color-8-background-color has-background\">Enum members can be accessed by their order using the <code><a href=\"https:\/\/blog.finxter.com\/python-list\/\">list()<\/a><\/code> conversion. The index refers to the order in which members are defined.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">print(list(Color)[0].value)\n# 1<\/pre>\n<h2 class=\"wp-block-heading\">Method 4: Python Enum Get Value by Variable<\/h2>\n<p><strong>Problem Formulation: <\/strong>How can you retrieve the value of an Enum member in Python using a variable that represents its name?<\/p>\n<p class=\"has-global-color-8-background-color has-background\">Similar to accessing by string, you can use the <code>getattr()<\/code> function with a variable holding the Enum member&#8217;s name.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var_name = \"GREEN\"\nprint(getattr(Color, var_name).value)\n# 2<\/pre>\n<\/p>\n<h2 class=\"wp-block-heading\">Method 5: Python Enum Get Value by Default<\/h2>\n<p><strong>Problem Formulation: <\/strong>How can you retrieve the default value (or the first value) of an Enum in Python?<\/p>\n<p class=\"has-global-color-8-background-color has-background\">By converting the Enum to a list and accessing the first element, you can retrieve the default or first value of the Enum.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">print(list(Color)[0].value)\n# 1<\/pre>\n<p class=\"has-base-2-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4a1.png\" alt=\"\ud83d\udca1\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Recommended<\/strong>: <a href=\"https:\/\/blog.finxter.com\/robotaxi-tycoon-scale-your-fleet-to-1m-a-python-mini-game-made-by-chatgpt\/\">Robotaxi Tycoon \u2013 Scale Your Fleet to $1M! A Python Mini Game Made By ChatGPT<\/a><\/p>\n<p>The post <a rel=\"nofollow\" href=\"https:\/\/blog.finxter.com\/python-enum-get-value-five-best-methods\/\">Python Enum Get Value &#8211; Five Best Methods<\/a> appeared first on <a rel=\"nofollow\" href=\"https:\/\/blog.finxter.com\">Be on the Right Side of Change<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>4\/5 &#8211; (1 vote) This article delves into the diverse methods of extracting values from Python&#8217;s Enum class. The Enum class in Python offers a platform to define named constants, known as enumerations. These enumerations can be accessed through various techniques: By Name\/Key: Access the enumeration directly through its designated name or key. By String\/String [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-134766","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/134766","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=134766"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/134766\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=134766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=134766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=134766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}