{"id":134914,"date":"2023-09-22T20:35:01","date_gmt":"2023-09-22T20:35:01","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=1651730"},"modified":"2023-09-22T20:35:01","modified_gmt":"2023-09-22T20:35:01","slug":"what-is-the-python-dunder-method-for-the-not-and-operator","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2023\/09\/22\/what-is-the-python-dunder-method-for-the-not-and-operator\/","title":{"rendered":"What is the Python Dunder Method for the \u201cnot and\u201d Operator?"},"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;1651730&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;0&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&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;0\\\/5 - (0 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;What is the Python Dunder Method for the &quot;not and&quot; Operator?&quot;,&quot;width&quot;:&quot;0&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: 0px;\">\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;\"> <span class=\"kksr-muted\">Rate this post<\/span> <\/div>\n<\/p><\/div>\n<p>In Python, &#8220;dunder&#8221; methods, short for &#8220;double underscore&#8221; methods, are special methods that allow developers to define the behavior of built-in operations for custom objects. For instance, when you use the <a href=\"https:\/\/blog.finxter.com\/python-arithmetic-operators\/\"><code>+<\/code> operator<\/a> to add two objects, Python internally calls the <code><a href=\"https:\/\/blog.finxter.com\/python-__add__\/\">__add__<\/a><\/code> method. Similarly, other operators have their corresponding dunder methods.<\/p>\n<p>However, the term &#8220;<code>not and<\/code>&#8221; operator might be a bit misleading, as there isn&#8217;t a direct &#8220;<code>not and<\/code>&#8221; operator in Python. <\/p>\n<p>Instead, Python provides individual operators for <code>not<\/code>, and <code>and<\/code>. But if we delve into the realm of <a href=\"https:\/\/blog.finxter.com\/python-bitwise-operators\/\">bitwise operations<\/a>, we find operators that might resemble this behavior: the <a href=\"https:\/\/blog.finxter.com\/python-bitwise-not-operator\/\">bitwise NOT (<code>~<\/code>)<\/a> and the <a href=\"https:\/\/blog.finxter.com\/python-bitwise-and-operator\/\">bitwise AND (<code>&amp;<\/code>)<\/a>.<\/p>\n<p>Let&#8217;s explore the dunder methods associated with these operators.<\/p>\n<h2 class=\"wp-block-heading\">Bitwise NOT (~) and its Dunder Method __invert__<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">The bitwise NOT operator flips the bits of a number. For a custom class, if you want to define or override the behavior of the <code>~<\/code> operator, you&#8217;d use the <code><a href=\"https:\/\/blog.finxter.com\/python-__invert__-magic-method\/\">__invert__<\/a><\/code> method.<\/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=\"\">class BitwiseNumber: def __init__(self, value): self.value = value def __invert__(self): return BitwiseNumber(~self.value) def __repr__(self): return str(self.value) number = BitwiseNumber(5)\nprint(~number) # Outputs: -6<\/pre>\n<p>In the above example, the <code>__invert__<\/code> method returns a new <code>BitwiseNumber<\/code> object with its value inverted.<\/p>\n<h2 class=\"wp-block-heading\">Bitwise AND (&amp;) and its Dunder Method __and__<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">The bitwise AND operator performs a bitwise AND operation between two numbers. For custom classes, the behavior of the <code>&amp;<\/code> operator can be defined or overridden using the <code><a href=\"https:\/\/blog.finxter.com\/python-__and__\/\">__and__<\/a><\/code> method.<\/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=\"\">class BitwiseNumber: def __init__(self, value): self.value = value def __and__(self, other): if isinstance(other, BitwiseNumber): return BitwiseNumber(self.value &amp; other.value) return NotImplemented def __repr__(self): return str(self.value) number1 = BitwiseNumber(5) # Binary: 101\nnumber2 = BitwiseNumber(3) # Binary: 011\nprint(number1 &amp; number2) # Outputs: 1 (Binary: 001)<\/pre>\n<p>In this example, the <code>__and__<\/code> method checks if the other object is an instance of <code>BitwiseNumber<\/code> and then performs a bitwise AND operation.<\/p>\n<h2 class=\"wp-block-heading\">TLDR<\/h2>\n<p>While there isn&#8217;t a direct &#8220;not and&#8221; operator in Python, leveraging the <code>__invert__<\/code> and <code>__and__<\/code> methods, you can define how the bitwise NOT and AND operations work for custom objects, respectively.<\/p>\n<p>The post <a rel=\"nofollow\" href=\"https:\/\/blog.finxter.com\/what-is-the-python-dunder-method-for-the-not-and-operator\/\">What is the Python Dunder Method for the &#8220;not and&#8221; Operator?<\/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>Rate this post In Python, &#8220;dunder&#8221; methods, short for &#8220;double underscore&#8221; methods, are special methods that allow developers to define the behavior of built-in operations for custom objects. For instance, when you use the + operator to add two objects, Python internally calls the __add__ method. Similarly, other operators have their corresponding dunder methods. However, [&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-134914","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\/134914","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=134914"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/134914\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=134914"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=134914"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=134914"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}