{"id":2933,"date":"2021-11-15T07:02:36","date_gmt":"2021-11-15T07:02:36","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=2933"},"modified":"2025-03-28T03:05:30","modified_gmt":"2025-03-28T03:05:30","slug":"python-enum-auto","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-oop\/python-enum-auto\/","title":{"rendered":"Python enum auto"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about the enum auto() function to generate unique values for enumeration members.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-enum-auto-function'>Introduction to the enum auto() function <a href=\"#introduction-to-the-enum-auto-function\" class=\"anchor\" id=\"introduction-to-the-enum-auto-function\" title=\"Anchor for Introduction to the enum auto() function\">#<\/a><\/h2>\n\n\n\n<p>The following example defines an <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-enumeration\/\">enumeration<\/a> with three members whose values are 1, 2, and 3:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> enum <span class=\"hljs-keyword\">import<\/span> Enum\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">State<\/span><span class=\"hljs-params\">(Enum)<\/span>:<\/span>\n    PENDING = <span class=\"hljs-number\">1<\/span>\n    FULFILLED = <span class=\"hljs-number\">2<\/span>\n    REJECTED = <span class=\"hljs-number\">3<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\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 <code>auto()<\/code> helper class in the <code>enum<\/code> module, which automatically generates unique values for the enumeration members. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> enum <span class=\"hljs-keyword\">import<\/span> Enum, auto\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">State<\/span><span class=\"hljs-params\">(Enum)<\/span>:<\/span>\n    PENDING = auto()\n    FULFILLED = auto()\n    REJECTED = auto()\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__str__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">f'<span class=\"hljs-subst\">{self.name(self.value)}<\/span>'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, import the <code>Enum<\/code> and <code>auto<\/code> classes from the <code>enum<\/code> module.<\/li>\n\n\n\n<li>Second, call the <code>auto()<\/code> to generate a unique value for each member of the <code>State<\/code> enumeration.<\/li>\n<\/ul>\n\n\n\n<p>By default, the <code>auto()<\/code> class generates a sequence of integer numbers starting from 1.<\/p>\n\n\n\n<p>The following shows the values of the <code>State<\/code> enumeration&#8217;s members:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">for<\/span> state <span class=\"hljs-keyword\">in<\/span> State:\n    print(state.name, state.value)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">PENDING <span class=\"hljs-number\">1<\/span>\nFULFILLED <span class=\"hljs-number\">2<\/span>\nREJECTED <span class=\"hljs-number\">3<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here&#8217;s the complete script:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">from enum import Enum, auto\n\n\nclass State(Enum):\n    PENDING = auto()\n    FULFILLED = auto()\n    REJECTED = auto()\n\n    def __str__(self):\n        return f'{self.name(self.value)}'\n    \nfor state in State:\n    print(state.name, state.value)<\/code><\/span><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=ZnJvbSBlbnVtIGltcG9ydCBFbnVtLCBhdXRvCgoKY2xhc3MgU3RhdGUoRW51bSk6CiAgICBQRU5ESU5HID0gYXV0bygpCiAgICBGVUxGSUxMRUQgPSBhdXRvKCkKICAgIFJFSkVDVEVEID0gYXV0bygpCgogICAgZGVmIF9fc3RyX18oc2VsZik6CiAgICAgICAgcmV0dXJuIGYne3NlbGYubmFtZShzZWxmLnZhbHVlKX0nCiAgICAKZm9yIHN0YXRlIGluIFN0YXRlOgogICAgcHJpbnQoc3RhdGUubmFtZSwgc3RhdGUudmFsdWUp\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='how-enum-auto-works'>How enum() auto works <a href=\"#how-enum-auto-works\" class=\"anchor\" id=\"how-enum-auto-works\" title=\"Anchor for How enum() auto works\">#<\/a><\/h2>\n\n\n\n<p>Technically, the <code>auto()<\/code> calls the <code>_generate_next_value_()<\/code> method to generate values for the members. Here&#8217;s the syntax of the <code>_generate_next_value_()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">_generate_next_value_(name, start, count, last_values)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The  <code>_generate_next_value_()<\/code> has the following parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>name<\/code> is the member&#8217;s name<\/li>\n\n\n\n<li><code>start<\/code> is the starting value of the enum members.<\/li>\n\n\n\n<li><code>count<\/code> is the number of enum members, including <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-enum-unique\/\">aliases<\/a>, that have been created.<\/li>\n\n\n\n<li><code>last_values<\/code> is a list of all preceding values used for the enum members.<\/li>\n<\/ul>\n\n\n\n<p>By default, the <code>_generate_next_value_()<\/code> 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&#8217;s possible to <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-overriding-method\/\">override<\/a> the <code>_generate_next_value_()<\/code> method to add a custom logic that generates unique values. If so, you need to place the <code>_generate_next_value_()<\/code> method before defining all the members.<\/p>\n\n\n\n<p>The following shows how to override the <code>_generate_next_value_()<\/code> method to generate values for members by using their names:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> enum <span class=\"hljs-keyword\">import<\/span> Enum, auto\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">State<\/span><span class=\"hljs-params\">(Enum)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">_generate_next_value_<\/span><span class=\"hljs-params\">(name, start, count, last_values)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> name.lower()\n\n    PENDING = auto()\n    FULFILLED = auto()\n    REJECTED = auto()\n\n\n<span class=\"hljs-keyword\">for<\/span> state <span class=\"hljs-keyword\">in<\/span> State:\n    print(state.name, state.value)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=ZnJvbSBlbnVtIGltcG9ydCBFbnVtLCBhdXRvCgoKY2xhc3MgU3RhdGUoRW51bSk6CiAgICBkZWYgX2dlbmVyYXRlX25leHRfdmFsdWVfKG5hbWUsIHN0YXJ0LCBjb3VudCwgbGFzdF92YWx1ZXMpOgogICAgICAgIHJldHVybiBuYW1lLmxvd2VyKCkKCiAgICBQRU5ESU5HID0gYXV0bygpCiAgICBGVUxGSUxMRUQgPSBhdXRvKCkKICAgIFJFSkVDVEVEID0gYXV0bygpCgoKZm9yIHN0YXRlIGluIFN0YXRlOgogICAgcHJpbnQoc3RhdGUubmFtZSwgc3RhdGUudmFsdWUp\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">PENDING pending\nFULFILLED fulfilled\nREJECTED rejected<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use enum <code>auto()<\/code> class to generate unique values for enumeration members.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2933\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-oop\/python-enum-auto\/\"\n\t\t\t\tdata-post-title=\"Python enum auto\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2933\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-oop\/python-enum-auto\/\"\n\t\t\t\tdata-post-title=\"Python enum auto\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn about the enum auto() function to generate unique values for enumeration members.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":417,"menu_order":30,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2933","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2933","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=2933"}],"version-history":[{"count":1,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2933\/revisions"}],"predecessor-version":[{"id":7170,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2933\/revisions\/7170"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/417"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=2933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}