{"id":188,"date":"2024-01-25T15:22:44","date_gmt":"2024-01-25T15:22:44","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=188"},"modified":"2024-01-25T15:22:45","modified_gmt":"2024-01-25T15:22:45","slug":"python-enum-auto","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/25\/python-enum-auto\/","title":{"rendered":"Python enum auto"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn about the enum auto() function to generate unique values for enumeration members.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the enum auto() function<\/h2>\n\n\n\n<p>The following example defines an\u00a0enumeration\u00a0with three members whose values are 1, 2, and 3:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>from enum import Enum class State(Enum): PENDING = 1 FULFILLED = 2 REJECTED = 3<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this example, we manually assign integer values to the members of the enumeration.<\/p>\n\n\n\n<p>To make it more convenient, Python 3.6 introduced the\u00a0<code>auto()<\/code>\u00a0helper class in the\u00a0<code>enum<\/code>\u00a0module, which automatically generates unique values for the enumeration members. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>from enum import Enum, auto class State(Enum): PENDING = auto() FULFILLED = auto() REJECTED = auto() def __str__(self): return f'{self.name(self.value)}'<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, import the&nbsp;<code>Enum<\/code>&nbsp;and&nbsp;<code>auto<\/code>&nbsp;classes from the&nbsp;<code>enum<\/code>&nbsp;module.<\/li>\n\n\n\n<li>Second, call the&nbsp;<code>auto()<\/code>&nbsp;to generate a unique value for each member of the&nbsp;<code>State<\/code>&nbsp;enumeration.<\/li>\n<\/ul>\n\n\n\n<p>By default, the&nbsp;<code>auto()<\/code>&nbsp;class generates a sequence of integer numbers starting from 1.<\/p>\n\n\n\n<p>The following shows the values of the\u00a0<code>State<\/code>\u00a0enumeration\u2019s members:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>for state in State: print(state.name, state.value)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>PENDING 1 FULFILLED 2 REJECTED 3<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How enum() auto works<\/h2>\n\n\n\n<p>Technically, the\u00a0<code>auto()<\/code>\u00a0calls the\u00a0<code>_generate_next_value_()<\/code>\u00a0method to generate values for the members. Here\u2019s the syntax of the\u00a0<code>_generate_next_value_()<\/code>\u00a0method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>_generate_next_value_(name, start, count, last_values)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>_generate_next_value_()<\/code>&nbsp;has the following parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>name<\/code>&nbsp;is the member\u2019s name<\/li>\n\n\n\n<li><code>start<\/code>&nbsp;is the starting value of the enum members.<\/li>\n\n\n\n<li><code>count<\/code>\u00a0is the number of enum members, including\u00a0aliases, that have been created.<\/li>\n\n\n\n<li><code>last_values<\/code>&nbsp;is a list of all preceding values used for the enum members.<\/li>\n<\/ul>\n\n\n\n<p>By default, the&nbsp;<code>_generate_next_value_()<\/code>&nbsp;generates the next number in a sequence of integers starting from one. However, Python may change this logic in the future.<\/p>\n\n\n\n<p>It\u2019s possible to\u00a0override\u00a0the\u00a0<code>_generate_next_value_()<\/code>\u00a0method to add a custom logic that generates unique values. If so, you need to place the\u00a0<code>_generate_next_value_()<\/code>\u00a0method before defining all the members.<\/p>\n\n\n\n<p>The following shows how to override the\u00a0<code>_generate_next_value_()<\/code>\u00a0method to generate values for members by using their names:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>from enum import Enum, auto class State(Enum): def _generate_next_value_(name, start, count, last_values): return name.lower() PENDING = auto() FULFILLED = auto() REJECTED = auto() for state in State: print(state.name, state.value)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>PENDING pending FULFILLED fulfilled REJECTED rejected<\/code><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn about the enum auto() function to generate unique values for enumeration members. Introduction to the enum auto() function The following example defines an\u00a0enumeration\u00a0with three members whose values are 1, 2, and 3: In this example, we manually assign integer values to the members of the enumeration. To make it [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[],"class_list":["post-188","post","type-post","status-publish","format-standard","hentry","category-5-enumeration"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/188","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=188"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/188\/revisions"}],"predecessor-version":[{"id":189,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/188\/revisions\/189"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}