{"id":2913,"date":"2021-11-14T09:56:31","date_gmt":"2021-11-14T09:56:31","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=2913"},"modified":"2025-03-28T02:52:37","modified_gmt":"2025-03-28T02:52:37","slug":"python-enum-class","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-oop\/python-enum-class\/","title":{"rendered":"Customizing and Extending Python Enum Class"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to customize and extend the custom Python enum classes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='customize-python-enum-classes'>Customize Python enum classes <a href=\"#customize-python-enum-classes\" class=\"anchor\" id=\"customize-python-enum-classes\" title=\"Anchor for Customize Python enum classes\">#<\/a><\/h2>\n\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-enumeration\/\">Python enumerations<\/a> are <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-class\/\">classes<\/a>. It means that you can add methods to them, or implement the dunder methods to customize their behaviors.<\/p>\n\n\n\n<p>The following example defines the <code>PaymentStatus<\/code> enumeration class:<\/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\">PaymentStatus<\/span><span class=\"hljs-params\">(Enum)<\/span>:<\/span>\n    PENDING = <span class=\"hljs-number\">1<\/span>\n    COMPLETED = <span class=\"hljs-number\">2<\/span>\n    REFUNDED = <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>The <code>PaymentStatus<\/code> enumeration has three members: <code>PENDING<\/code>, <code>COMPLETED<\/code>, and <code>REFUND<\/code>.<\/p>\n\n\n\n<p>The following displays the member of the <code>PaymentStatus<\/code>&#8216; member:<\/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\">print(PaymentStatus.PENDING)<\/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>It shows the following:<\/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\">PaymentStatus.PENDING<\/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>To customize how the <code>PaymentStatus<\/code> member&#8217;s is represented in the string, you can implement the <code><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-__str__\/\">__str__<\/a><\/code> method. For example:<\/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\"><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\">PaymentStatus<\/span><span class=\"hljs-params\">(Enum)<\/span>:<\/span>\n    PENDING = <span class=\"hljs-number\">1<\/span>\n    COMPLETED = <span class=\"hljs-number\">2<\/span>\n    REFUNDED = <span class=\"hljs-number\">3<\/span>\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.lower()}<\/span>(<span class=\"hljs-subst\">{self.value}<\/span>)'<\/span>\n\n\nprint(PaymentStatus.PENDING)<\/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><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=ZnJvbSBlbnVtIGltcG9ydCBFbnVtCgoKY2xhc3MgUGF5bWVudFN0YXR1cyhFbnVtKToKICAgIFBFTkRJTkcgPSAxCiAgICBDT01QTEVURUQgPSAyCiAgICBSRUZVTkRFRCA9IDMKCiAgICBkZWYgX19zdHJfXyhzZWxmKToKICAgICAgICByZXR1cm4gZid7c2VsZi5uYW1lLmxvd2VyKCl9KHtzZWxmLnZhbHVlfSknCgoKcHJpbnQoUGF5bWVudFN0YXR1cy5QRU5ESU5HKQ%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Now, it returns the following string:<\/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\">pending(<span class=\"hljs-number\">1<\/span>)<\/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<h3 class=\"wp-block-heading\" id='implementing-__eq__-method'>Implementing __eq__ method <a href=\"#implementing-__eq__-method\" class=\"anchor\" id=\"implementing-__eq__-method\" title=\"Anchor for Implementing __eq__ method\">#<\/a><\/h3>\n\n\n\n<p>The following attempts to compare a member of the <code>PaymentStatus<\/code> enum class with an integer:<\/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\">if<\/span> PaymentStatus.PENDING == <span class=\"hljs-number\">1<\/span>:\n    print(<span class=\"hljs-string\">'The payment is pending.'<\/span>)<\/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>It shows nothing because the <code>PaymentStatus.PENDING<\/code> is not equal to integer 1.<\/p>\n\n\n\n<p>To allow the comparison between <code>PaymentStatus<\/code> member and an integer, you can implement the <code><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-__eq__\/\">__eq__<\/a><\/code> method like this:<\/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\"><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\">PaymentStatus<\/span><span class=\"hljs-params\">(Enum)<\/span>:<\/span>\n    PENDING = <span class=\"hljs-number\">1<\/span>\n    COMPLETED = <span class=\"hljs-number\">2<\/span>\n    REFUNDED = <span class=\"hljs-number\">3<\/span>\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.lower()}<\/span>(<span class=\"hljs-subst\">{self.value}<\/span>)'<\/span>\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__eq__<\/span><span class=\"hljs-params\">(self, other)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">if<\/span> isinstance(other, int):\n            <span class=\"hljs-keyword\">return<\/span> self.value == other\n\n        <span class=\"hljs-keyword\">if<\/span> isinstance(other, PaymentStatus):\n            <span class=\"hljs-keyword\">return<\/span> self <span class=\"hljs-keyword\">is<\/span> other\n\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">False<\/span>\n\n\n<span class=\"hljs-keyword\">if<\/span> PaymentStatus.PENDING == <span class=\"hljs-number\">1<\/span>:\n    print(<span class=\"hljs-string\">'The payment is pending.'<\/span>)<\/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<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=ZnJvbSBlbnVtIGltcG9ydCBFbnVtCgoKY2xhc3MgUGF5bWVudFN0YXR1cyhFbnVtKToKICAgIFBFTkRJTkcgPSAxCiAgICBDT01QTEVURUQgPSAyCiAgICBSRUZVTkRFRCA9IDMKCiAgICBkZWYgX19zdHJfXyhzZWxmKToKICAgICAgICByZXR1cm4gZid7c2VsZi5uYW1lLmxvd2VyKCl9KHtzZWxmLnZhbHVlfSknCgogICAgZGVmIF9fZXFfXyhzZWxmLCBvdGhlcik6CiAgICAgICAgaWYgaXNpbnN0YW5jZShvdGhlciwgaW50KToKICAgICAgICAgICAgcmV0dXJuIHNlbGYudmFsdWUgPT0gb3RoZXIKCiAgICAgICAgaWYgaXNpbnN0YW5jZShvdGhlciwgUGF5bWVudFN0YXR1cyk6CiAgICAgICAgICAgIHJldHVybiBzZWxmIGlzIG90aGVyCgogICAgICAgIHJldHVybiBGYWxzZQoKCmlmIFBheW1lbnRTdGF0dXMuUEVORElORyA9PSAxOgogICAgcHJpbnQoJ1RoZSBwYXltZW50IGlzIHBlbmRpbmcuJyk%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">The payment is pending.<\/code><\/span><\/pre>\n\n\n<p>In the <code>__eq__<\/code> method:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If the value to compare is an integer, it compares the value of the member with the integer.<\/li>\n\n\n\n<li>If the value to compare is an instance of the <code>PaymentStatus<\/code> enumeration, it compares the value with the member of the <code>PaymentStatus<\/code> member using the <code>is<\/code> operator.<\/li>\n\n\n\n<li>Otherwise, it returns <code>False<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>The program works as expected and returns the following output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">The payment <span class=\"hljs-keyword\">is<\/span> pending.<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><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='implementing-__lt__-method'>Implementing __lt__ method <a href=\"#implementing-__lt__-method\" class=\"anchor\" id=\"implementing-__lt__-method\" title=\"Anchor for Implementing __lt__ method\">#<\/a><\/h2>\n\n\n\n<p>Suppose that you want the members of the <code>PaymentStatus<\/code> to follow have a sort order based on their value. And you also want to compare them with an integer. <\/p>\n\n\n\n<p>To do that, you can implement the <code>__lt__<\/code> method and use the <code>@total_ordering<\/code> decorator from the <code>functools<\/code> module:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" 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<span class=\"hljs-keyword\">from<\/span> functools <span class=\"hljs-keyword\">import<\/span> total_ordering\n\n\n<span class=\"hljs-meta\">@total_ordering<\/span>\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">PaymentStatus<\/span><span class=\"hljs-params\">(Enum)<\/span>:<\/span>\n    PENDING = <span class=\"hljs-number\">1<\/span>\n    COMPLETED = <span class=\"hljs-number\">2<\/span>\n    REFUNDED = <span class=\"hljs-number\">3<\/span>\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.lower()}<\/span>(<span class=\"hljs-subst\">{self.value}<\/span>)'<\/span>\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__eq__<\/span><span class=\"hljs-params\">(self, other)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">if<\/span> isinstance(other, int):\n            <span class=\"hljs-keyword\">return<\/span> self.value == other\n\n        <span class=\"hljs-keyword\">if<\/span> isinstance(other, PaymentStatus):\n            <span class=\"hljs-keyword\">return<\/span> self <span class=\"hljs-keyword\">is<\/span> other\n\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">False<\/span>\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__lt__<\/span><span class=\"hljs-params\">(self, other)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">if<\/span> isinstance(other, int):\n            <span class=\"hljs-keyword\">return<\/span> self.value &lt; other\n\n        <span class=\"hljs-keyword\">if<\/span> isinstance(other, PaymentStatus):\n            <span class=\"hljs-keyword\">return<\/span> self.value &lt; other.value\n\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">False<\/span>\n\n\n<span class=\"hljs-comment\"># compare with an integer<\/span>\nstatus = <span class=\"hljs-number\">1<\/span>\n<span class=\"hljs-keyword\">if<\/span> status &lt; PaymentStatus.COMPLETED:\n    print(<span class=\"hljs-string\">'The payment has not completed'<\/span>)\n\n<span class=\"hljs-comment\"># compare with another member<\/span>\nstatus = PaymentStatus.PENDING\n<span class=\"hljs-keyword\">if<\/span> status &gt;= PaymentStatus.COMPLETED:\n    print(<span class=\"hljs-string\">'The payment is not pending'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><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=ZnJvbSBlbnVtIGltcG9ydCBFbnVtCmZyb20gZnVuY3Rvb2xzIGltcG9ydCB0b3RhbF9vcmRlcmluZwoKCkB0b3RhbF9vcmRlcmluZwpjbGFzcyBQYXltZW50U3RhdHVzKEVudW0pOgogICAgUEVORElORyA9IDEKICAgIENPTVBMRVRFRCA9IDIKICAgIFJFRlVOREVEID0gMwoKICAgIGRlZiBfX3N0cl9fKHNlbGYpOgogICAgICAgIHJldHVybiBmJ3tzZWxmLm5hbWUubG93ZXIoKX0oe3NlbGYudmFsdWV9KScKCiAgICBkZWYgX19lcV9fKHNlbGYsIG90aGVyKToKICAgICAgICBpZiBpc2luc3RhbmNlKG90aGVyLCBpbnQpOgogICAgICAgICAgICByZXR1cm4gc2VsZi52YWx1ZSA9PSBvdGhlcgoKICAgICAgICBpZiBpc2luc3RhbmNlKG90aGVyLCBQYXltZW50U3RhdHVzKToKICAgICAgICAgICAgcmV0dXJuIHNlbGYgaXMgb3RoZXIKCiAgICAgICAgcmV0dXJuIEZhbHNlCgogICAgZGVmIF9fbHRfXyhzZWxmLCBvdGhlcik6CiAgICAgICAgaWYgaXNpbnN0YW5jZShvdGhlciwgaW50KToKICAgICAgICAgICAgcmV0dXJuIHNlbGYudmFsdWUgPCBvdGhlcgoKICAgICAgICBpZiBpc2luc3RhbmNlKG90aGVyLCBQYXltZW50U3RhdHVzKToKICAgICAgICAgICAgcmV0dXJuIHNlbGYudmFsdWUgPCBvdGhlci52YWx1ZQoKICAgICAgICByZXR1cm4gRmFsc2UKCgojIGNvbXBhcmUgd2l0aCBhbiBpbnRlZ2VyCnN0YXR1cyA9IDEKaWYgc3RhdHVzIDwgUGF5bWVudFN0YXR1cy5DT01QTEVURUQ6CiAgICBwcmludCgnVGhlIHBheW1lbnQgaGFzIG5vdCBjb21wbGV0ZWQnKQoKIyBjb21wYXJlIHdpdGggYW5vdGhlciBtZW1iZXIKc3RhdHVzID0gUGF5bWVudFN0YXR1cy5QRU5ESU5HCmlmIHN0YXR1cyA%2BPSBQYXltZW50U3RhdHVzLkNPTVBMRVRFRDoKICAgIHByaW50KCdUaGUgcGF5bWVudCBpcyBub3QgcGVuZGluZycp\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">The payment has not completed<\/code><\/span><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='implementing-the-__bool__-method'>Implementing the __bool__ method <a href=\"#implementing-the-__bool__-method\" class=\"anchor\" id=\"implementing-the-__bool__-method\" title=\"Anchor for Implementing the __bool__ method\">#<\/a><\/h3>\n\n\n\n<p>By default, all members of an enumeration are truthy. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">for<\/span> member <span class=\"hljs-keyword\">in<\/span> PaymentStatus:\n    print(member, bool(member))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><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>To customize this behavior, you need to implement the <code>__bool__<\/code> method. Suppose you want the <code>COMPLETED<\/code> and <code>REFUNDED<\/code> members to be True while the <code>PENDING<\/code> to be <code>False<\/code>. <\/p>\n\n\n\n<p>The following shows how to implement this logic:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" 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<span class=\"hljs-keyword\">from<\/span> functools <span class=\"hljs-keyword\">import<\/span> total_ordering\n\n\n<span class=\"hljs-meta\">@total_ordering<\/span>\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">PaymentStatus<\/span><span class=\"hljs-params\">(Enum)<\/span>:<\/span>\n    PENDING = <span class=\"hljs-number\">1<\/span>\n    COMPLETED = <span class=\"hljs-number\">2<\/span>\n    REFUNDED = <span class=\"hljs-number\">3<\/span>\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.lower()}<\/span>(<span class=\"hljs-subst\">{self.value}<\/span>)'<\/span>\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__eq__<\/span><span class=\"hljs-params\">(self, other)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">if<\/span> isinstance(other, int):\n            <span class=\"hljs-keyword\">return<\/span> self.value == other\n\n        <span class=\"hljs-keyword\">if<\/span> isinstance(other, PaymentStatus):\n            <span class=\"hljs-keyword\">return<\/span> self <span class=\"hljs-keyword\">is<\/span> other\n\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">False<\/span>\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__lt__<\/span><span class=\"hljs-params\">(self, other)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">if<\/span> isinstance(other, int):\n            <span class=\"hljs-keyword\">return<\/span> self.value &lt; other\n\n        <span class=\"hljs-keyword\">if<\/span> isinstance(other, PaymentStatus):\n            <span class=\"hljs-keyword\">return<\/span> self.value &lt; other.value\n\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">False<\/span>\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__bool__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">if<\/span> self <span class=\"hljs-keyword\">is<\/span> self.COMPLETED:\n            <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">True<\/span>\n\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">False<\/span>\n\n\n<span class=\"hljs-keyword\">for<\/span> member <span class=\"hljs-keyword\">in<\/span> PaymentStatus:\n    print(member, bool(member))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><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=ZnJvbSBlbnVtIGltcG9ydCBFbnVtCmZyb20gZnVuY3Rvb2xzIGltcG9ydCB0b3RhbF9vcmRlcmluZwoKCkB0b3RhbF9vcmRlcmluZwpjbGFzcyBQYXltZW50U3RhdHVzKEVudW0pOgogICAgUEVORElORyA9IDEKICAgIENPTVBMRVRFRCA9IDIKICAgIFJFRlVOREVEID0gMwoKICAgIGRlZiBfX3N0cl9fKHNlbGYpOgogICAgICAgIHJldHVybiBmJ3tzZWxmLm5hbWUubG93ZXIoKX0oe3NlbGYudmFsdWV9KScKCiAgICBkZWYgX19lcV9fKHNlbGYsIG90aGVyKToKICAgICAgICBpZiBpc2luc3RhbmNlKG90aGVyLCBpbnQpOgogICAgICAgICAgICByZXR1cm4gc2VsZi52YWx1ZSA9PSBvdGhlcgoKICAgICAgICBpZiBpc2luc3RhbmNlKG90aGVyLCBQYXltZW50U3RhdHVzKToKICAgICAgICAgICAgcmV0dXJuIHNlbGYgaXMgb3RoZXIKCiAgICAgICAgcmV0dXJuIEZhbHNlCgogICAgZGVmIF9fbHRfXyhzZWxmLCBvdGhlcik6CiAgICAgICAgaWYgaXNpbnN0YW5jZShvdGhlciwgaW50KToKICAgICAgICAgICAgcmV0dXJuIHNlbGYudmFsdWUgPCBvdGhlcgoKICAgICAgICBpZiBpc2luc3RhbmNlKG90aGVyLCBQYXltZW50U3RhdHVzKToKICAgICAgICAgICAgcmV0dXJuIHNlbGYudmFsdWUgPCBvdGhlci52YWx1ZQoKICAgICAgICByZXR1cm4gRmFsc2UKCiAgICBkZWYgX19ib29sX18oc2VsZik6CiAgICAgICAgaWYgc2VsZiBpcyBzZWxmLkNPTVBMRVRFRDoKICAgICAgICAgICAgcmV0dXJuIFRydWUKCiAgICAgICAgcmV0dXJuIEZhbHNlCgoKZm9yIG1lbWJlciBpbiBQYXltZW50U3RhdHVzOgogICAgcHJpbnQobWVtYmVyLCBib29sKG1lbWJlcikp\" 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-12\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">pending(1) False\ncompleted(2) True\nrefunded(3) False<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='extend-python-enum-classes'>Extend Python enum classes <a href=\"#extend-python-enum-classes\" class=\"anchor\" id=\"extend-python-enum-classes\" title=\"Anchor for Extend Python enum classes\">#<\/a><\/h2>\n\n\n\n<p>Python doesn&#8217;t allow you to extend an enum class unless it has no member. However, this is not a limitation. Because you can define a base class that has methods but no member and then extend this base class. For example:<\/p>\n\n\n\n<p>First, define the <code>OrderedEnum<\/code> base class that orders the members by their values:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" 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<span class=\"hljs-keyword\">from<\/span> functools <span class=\"hljs-keyword\">import<\/span> total_ordering\n\n\n<span class=\"hljs-meta\">@total_ordering<\/span>\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">OrderedEnum<\/span><span class=\"hljs-params\">(Enum)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__lt__<\/span><span class=\"hljs-params\">(self, other)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">if<\/span> isinstance(other, OrderedEnum):\n            <span class=\"hljs-keyword\">return<\/span> self.value &lt; other.value\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-built_in\">NotImplemented<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><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>Second, define the <code>ApprovalStatus<\/code> that extends the <code>OrderedEnum<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">ApprovalStatus<\/span><span class=\"hljs-params\">(OrderedEnum)<\/span>:<\/span>\n    PENDING = <span class=\"hljs-number\">1<\/span>\n    IN_PROGRESS = <span class=\"hljs-number\">2<\/span>\n    APPROVED = <span class=\"hljs-number\">3<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><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>Third, compare the members of the <code>ApprovalStatus<\/code> enum class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">status = ApprovalStatus(<span class=\"hljs-number\">2<\/span>)\n<span class=\"hljs-keyword\">if<\/span> status &lt; ApprovalStatus.APPROVED:\n    print(<span class=\"hljs-string\">'The request has not been approved.'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><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-16\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">The request has not been approved.<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Put it all together:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" 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<span class=\"hljs-keyword\">from<\/span> functools <span class=\"hljs-keyword\">import<\/span> total_ordering\n\n\n<span class=\"hljs-meta\">@total_ordering<\/span>\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">OrderedEnum<\/span><span class=\"hljs-params\">(Enum)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__lt__<\/span><span class=\"hljs-params\">(self, other)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">if<\/span> isinstance(other, OrderedEnum):\n            <span class=\"hljs-keyword\">return<\/span> self.value &lt; other.value\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-built_in\">NotImplemented<\/span>\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">ApprovalStatus<\/span><span class=\"hljs-params\">(OrderedEnum)<\/span>:<\/span>\n    PENDING = <span class=\"hljs-number\">1<\/span>\n    IN_PROGRESS = <span class=\"hljs-number\">2<\/span>\n    APPROVED = <span class=\"hljs-number\">3<\/span>\n\n\nstatus = ApprovalStatus(<span class=\"hljs-number\">2<\/span>)\n<span class=\"hljs-keyword\">if<\/span> status &lt; ApprovalStatus.APPROVED:\n    print(<span class=\"hljs-string\">'The request has not been approved.'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><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=ZnJvbSBlbnVtIGltcG9ydCBFbnVtCmZyb20gZnVuY3Rvb2xzIGltcG9ydCB0b3RhbF9vcmRlcmluZwoKCkB0b3RhbF9vcmRlcmluZwpjbGFzcyBPcmRlcmVkRW51bShFbnVtKToKICAgIGRlZiBfX2x0X18oc2VsZiwgb3RoZXIpOgogICAgICAgIGlmIGlzaW5zdGFuY2Uob3RoZXIsIE9yZGVyZWRFbnVtKToKICAgICAgICAgICAgcmV0dXJuIHNlbGYudmFsdWUgPCBvdGhlci52YWx1ZQogICAgICAgIHJldHVybiBOb3RJbXBsZW1lbnRlZAoKCmNsYXNzIEFwcHJvdmFsU3RhdHVzKE9yZGVyZWRFbnVtKToKICAgIFBFTkRJTkcgPSAxCiAgICBJTl9QUk9HUkVTUyA9IDIKICAgIEFQUFJPVkVEID0gMwoKCnN0YXR1cyA9IEFwcHJvdmFsU3RhdHVzKDIpCmlmIHN0YXR1cyA8IEFwcHJvdmFsU3RhdHVzLkFQUFJPVkVEOgogICAgcHJpbnQoJ1RoZSByZXF1ZXN0IGhhcyBub3QgYmVlbiBhcHByb3ZlZC4nKQ%3D%3D\" 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-18\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">The request has <span class=\"hljs-keyword\">not<\/span> been approved.<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><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>Implement dunder methods to customize the behavior of Python enum classes.<\/li>\n\n\n\n<li>Define an emum class with no members and methods and extends this base class.<\/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=\"2913\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-oop\/python-enum-class\/\"\n\t\t\t\tdata-post-title=\"Customizing and Extending Python Enum Class\"\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=\"2913\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-oop\/python-enum-class\/\"\n\t\t\t\tdata-post-title=\"Customizing and Extending Python Enum Class\"\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>Summary: in this tutorial, you&#8217;ll learn how to customize and extend the custom Python enum classes. Customize Python enum classes # Python enumerations are classes. It means that you can add methods to them, or implement the dunder methods to customize their behaviors. The following example defines the PaymentStatus enumeration class: The PaymentStatus enumeration has [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":417,"menu_order":29,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2913","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2913","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=2913"}],"version-history":[{"count":1,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2913\/revisions"}],"predecessor-version":[{"id":7169,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2913\/revisions\/7169"}],"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=2913"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}