{"id":1466,"date":"2020-06-26T13:45:58","date_gmt":"2020-06-26T01:45:58","guid":{"rendered":"http:\/\/p-s.co.nz\/wordpress\/?p=1466"},"modified":"2021-03-16T07:55:12","modified_gmt":"2021-03-15T19:55:12","slug":"python-enum-gotcha","status":"publish","type":"post","link":"http:\/\/p-s.co.nz\/wordpress\/python-enum-gotcha\/","title":{"rendered":"Python Enum Gotcha"},"content":{"rendered":"\n<p>There is plenty of useful information in <a href=\"https:\/\/florian-dahlitz.de\/blog\/why-you-should-use-more-enums-in-python\">Why You Should Use More Enums In Python &#8211; A gentle introduction to enumerations in Python<\/a>. After reading it I decided to look into using Enums more. Unfortunately I hit a major Gotcha quite quickly.<\/p>\n\n\n\n<p>Basically, comparisons don&#8217;t work with vanilla Enum (unlike IntEnum). Checking the official Python documentation seemed to confirm this understanding:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>&#8220;Comparisons against non-enumeration values will always compare not equal (again, IntEnum was explicitly designed to behave differently &#8230;)&#8221; &#8212; <a href=\"https:\/\/docs.python.org\/3\/library\/enum.html\">https:\/\/docs.python.org\/3\/library\/enum.html<\/a><\/p><\/blockquote>\n\n\n\n<p>In the snippet below, Pieces subclasses vanilla Enum and has, at least from my point of view, very unexpected results. Pieces2 is based on IntEnum and behaves as might be expected.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import enum\n\nclass Pieces(enum.Enum):\n    <code>PAWN = 8<\/code>\n    <code>ROOK = 2<\/code>\n    <code>BISHOP = 2<\/code>\n\n\nprint(2 == Pieces.ROOK) ## False WAT?! Always not equal to non enums\nprint(Pieces.ROOK == 2) ## False WAT?!\nprint(Pieces.BISHOP == Pieces.ROOK) ## True\nprint(Pieces.PAWN == Pieces.ROOK) ## False\n\n\nclass Pieces2(enum.IntEnum):\n<code>    PAWN = 8<\/code>\n    <code>ROOK = 2<\/code>\n    <code>BISHOP = 2<\/code>\n\n\nprint(2 == Pieces2.ROOK) ## True\nprint(Pieces2.ROOK == 2) ## True\nprint(Pieces2.BISHOP == Pieces2.ROOK) ## True\nprint(Pieces2.PAWN == Pieces2.ROOK) ## False<\/pre>\n\n\n\n<p>It is easy to imagine this behaviour creating baffling bugs. Interesting.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There is plenty of useful information in Why You Should Use More Enums In Python &#8211; A gentle introduction to enumerations in Python. After reading it I decided to look into using Enums more. Unfortunately I hit a major Gotcha &hellip; <a href=\"http:\/\/p-s.co.nz\/wordpress\/python-enum-gotcha\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[18,15],"class_list":["post-1466","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-programming","tag-python"],"_links":{"self":[{"href":"http:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/posts\/1466"}],"collection":[{"href":"http:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/comments?post=1466"}],"version-history":[{"count":2,"href":"http:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/posts\/1466\/revisions"}],"predecessor-version":[{"id":1469,"href":"http:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/posts\/1466\/revisions\/1469"}],"wp:attachment":[{"href":"http:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/media?parent=1466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/categories?post=1466"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/tags?post=1466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}