

{"id":7254,"date":"2018-02-05T16:28:09","date_gmt":"2018-02-05T10:58:09","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=7254"},"modified":"2026-04-20T15:29:21","modified_gmt":"2026-04-20T09:59:21","slug":"python-operator-overloading","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-operator-overloading\/","title":{"rendered":"Python Operator Overloading and Python Magic Methods"},"content":{"rendered":"<p>In this Python tutorial, we are going to discuss Python Operator Overloading, examples of operator overloading in Python, and Python magic methods with some operators: Python binary operators, Python comparison operators, Python unary operators, and Python extended assignments.<\/p>\n<p>In the programming world, operator overloading is also called <strong>Operator Ad-hoc Polymorphism<\/strong>.<\/p>\n<p>Indeed, it is a case of polymorphism where different operators have different implementations based on their arguments.<\/p>\n<p>This is either defined by the programmer, by the programming language, or both.<\/p>\n<p>So, let&#8217;s start the Magic Methods i Tutorial.<\/p>\n<h3>What is Python Operators Overloading?<\/h3>\n<p><strong>Advantages of operator overloading in Python:<\/strong><\/p>\n<ul>\n<li><strong>Custom behavior:<\/strong> You can explain how the operators work for your own classes.<\/li>\n<li><strong>Less code:<\/strong> There is no need to write the same functions again and again.<\/li>\n<li><strong>More useful:<\/strong> when new data types are used in operators, it becomes more meaningful.<\/li>\n<\/ul>\n<p>In Python, an operator lets us perform an operation\/procedure on one or more operands(values).<\/p>\n<p>Operator overloading must stay clear. Readers should guess what v1 + v2 means without surprise. When used wisely, it makes custom types act like numbers, strings, or lists, reducing friction while coding.<\/p>\n<p>Let\u2019s take an example.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; 42+1<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">43<\/div>\n<p>Here, we performed the addition of two numbers using the addition operator.<\/p>\n<p>You know that we can apply this operator to <strong>Python string<\/strong> too. In that case, we call it the concatenation operator.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; '42'+'1'<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;421&#8217;<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; 'hello'+' '+'world'<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;hello world&#8217;<\/div>\n<p>And then when we do this to a <strong>Python list<\/strong>, we concatenate two lists.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; [1,2,3]+[4,5,6]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[1, 2, 3, 4, 5, 6]<\/div>\n<p>Python does this implicitly, but what for when you want to apply this operator to your own class? Can we? Let\u2019s give it a try.<\/p>\n<p>In this Python operator overloading tutorial, we take a class \u2018myfloat\u2019 to represent floating-point numbers.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; class myfloat: \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 def __init__(self,whole,fraction): \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 self.whole=whole \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0  self.fraction=fraction \u00a0\u00a0\u00a0\u00a0\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0def shownumber(self): \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0print(f\"I am {self.whole}.{self.fraction}\")\r\n&gt;&gt;&gt; obj1=myfloat(3,7)\r\n&gt;&gt;&gt; obj1.shownumber()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">I am 3.7<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; obj2=myfloat(3,3)\r\n&gt;&gt;&gt; obj2.shownumber()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">I am 3.3<\/div>\n<p>Now, let\u2019s try adding two objects.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; obj1+obj2<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):<\/p>\n<p>File &#8220;&lt;pyshell#24&gt;&#8221;, line 1, in &lt;module&gt; obj1+obj2TypeError: unsupported operand type(s) for +: &#8216;myfloat&#8217; and &#8216;myfloat&#8217;<\/p>\n<\/div>\n<p>As you can see, this raised a TypeError. But don\u2019t fret; we can do this. We\u2019ll discuss in a later section.<\/p>\n<h3>Python Magic Methods<\/h3>\n<p>We observe that Python methods have double underscores before and after their names. These are special methods and are also called \u2018dunders\u2019.<\/p>\n<p>Magic methods in Python help us implement functionality that a normal method can\u2019t represent.<\/p>\n<p>By now, we have come across only one magic method- __init__(). But we can, in fact, define our own magic methods to implement operator overloading in Python.<\/p>\n<p>With this, we can define these operators to work on our custom classes. Some of these are-<\/p>\n<h4>1. Python Binary Operators<\/h4>\n<p>__add__ for +<\/p>\n<p>__sub__ for \u2013<\/p>\n<p>__mul__ for *<\/p>\n<p>__truediv__ for \/<\/p>\n<p>__floordiv__ for \/\/<\/p>\n<p>__mod__ for %<\/p>\n<p>__pow__ for **<\/p>\n<p>__and__ for &amp;<\/p>\n<p>__xor__ for ^<\/p>\n<p>__or__ for |<\/p>\n<p>__lshift__ for &lt;&lt;<\/p>\n<p>__rshift__ for &gt;&gt;<\/p>\n<h4>2. Python Extended Assignments<\/h4>\n<p>__iadd__ for +=<\/p>\n<p>__isub__ for -=<\/p>\n<p>__imul__ for *=<\/p>\n<p>__idiv__ for \/=<\/p>\n<p>__ifloordiv__ for \/\/=<\/p>\n<p>__imod__ for %=<\/p>\n<p>__ipow__ for **=<\/p>\n<p>__ilshift__ for &lt;&lt;=<\/p>\n<p>__irshift__ for &gt;&gt;=<\/p>\n<p>__iand__ for &amp;=<\/p>\n<p>__ixor__ for ^=<\/p>\n<p>__ior__ for |=<\/p>\n<h4>3. Python Unary Operators<\/h4>\n<p>__neg__ for \u2013<\/p>\n<p>__pos__ for +<\/p>\n<p>__abs__ for abs()<\/p>\n<p>__invert__ for ~<\/p>\n<p>__complex__ for complex()<\/p>\n<p>__int__ for int()<\/p>\n<p>__long__ for long()<\/p>\n<p>__float__ for float()<\/p>\n<p>__oct__ for oct()<\/p>\n<p>__hex__ for hex()<\/p>\n<h4>4. Python Comparison Operators<\/h4>\n<p>__lt__ for &lt;<\/p>\n<p>__le__ for &lt;=<\/p>\n<p>__eq__ for ==<\/p>\n<p>__ne__ for !=<\/p>\n<p>__ge__ for &gt;=<\/p>\n<p>__gt__ for &gt;<\/p>\n<p>Others include __radd__ for reverse add.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; class myclass:\r\n   def __init__(self,age):\r\n           self.age=age\r\n   def __add__(self,other):\r\n           return self.age+other\r\n   def __radd__(self,other):\r\n           return self.age+other\r\n&gt;&gt;&gt; a=myclass(1)\r\n&gt;&gt;&gt; a+2<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">3<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; 2+a<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">3<\/div>\n<p>If the interpreter cannot add left to right, it will call\u00a0 __radd__() instead. Here, radd is in reverse\/reflected add.<\/p>\n<h3>Python Operator Overloading Example<\/h3>\n<p>To be able to add our <strong>Python objects<\/strong> obj1 and obj2 for class \u2018myfloat\u2019, we can do the following.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; class myfloat: \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n      def __init__(self,whole,fraction): \u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 self.whole=whole \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n           self.fraction=fraction \u00a0\u00a0\u00a0\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 def shownumber(self): \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0  print(f\"I am {self.whole}.{self.fraction}\") \u00a0\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0def __add__(self,other): \u00a0\u00a0\u00a0\u00a0\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (self.fraction+other.fraction)&gt;9: \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n                return myfloat(self.whole+other.whole+1,self.fraction+other.fraction-10) \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return myfloat(self.whole+other.whole,self.fraction+other.fraction)<\/pre>\n<p>Here, we added another method __add__, which takes two parameters (\u2018self\u2019 and \u2018other\u2019) for the two objects.<\/p>\n<p>Then, it checks if the sum of the fraction parts of both objects is greater than 9. In that case, it transfers a 10 to the \u2018whole\u2019 part as a 1.<\/p>\n<p>This is for the carry. It then returns an object with the sums of the whole and fraction parts of both objects.<\/p>\n<p>However, if the condition isn\u2019t met, it simply returns an object with the sums.<\/p>\n<p>Let\u2019s create objects obj1 and obj2 again, and try adding them.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; obj1=myfloat(3,7)\r\n&gt;&gt;&gt; obj1.shownumber()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">I am 3.7<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; obj2=myfloat(3,3)\r\n&gt;&gt;&gt; obj2.shownumber()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">I am 3.3<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; result=obj1+obj2\r\n&gt;&gt;&gt; print(f\"I am {result.whole}.{result.fraction}\")<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">I am 7.0<\/div>\n<p>As you can see, it works absolutely fine now and lets us add two objects of class \u2018myfloat\u2019.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; result<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&lt;__main__.myfloat object at 0x0572FD10&gt;<\/div>\n<p>This is the resulting object of adding obj1 and obj2.<\/p>\n<p>Here, the interpreter translates obj1+obj2 to obj1.__add__(obj2).<\/p>\n<h3>More Examples of Python Operator Overloading<\/h3>\n<p>To really understand something, once is never enough. So, let\u2019s take another example of Operator overloading in Python.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; class itspower: \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n        def __init__(self,x): \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 self.x=x \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n        def __pow__(self,other): \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n     \u00a0         return self.x**other.x\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n&gt;&gt;&gt; a=itspower(2)\r\n&gt;&gt;&gt; b=itspower(10)\r\n&gt;&gt;&gt; a**b<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1024<\/div>\n<p>In this, we take a class \u2018itspower\u2019 and two methods __init__ and __pow__.<\/p>\n<p>__pow__ takes two objects and returns the \u2018x\u2019 of first raised to the power of the \u2018x\u2019 of the second.<\/p>\n<p>When we type a**b, the interpreter converts it implicitly to a.__pow__(b).<\/p>\n<p>Now, let\u2019s take another example to demonstrate few more such magic methods.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; class Person:\r\n      def __init__(self,name,age):\r\n         self.name=name\r\n         self.age=age\r\n      def __gt__(self,other):\r\n         if self.age&gt;other.age:\r\n                return True\r\n         return False\r\n      def __abs__(self):\r\n         return abs(self.age)\r\n      def __iadd__(self,other):\r\n         return self.age+other.age\r\n&gt;&gt;&gt; Nick=Person('Nick',7)\r\n&gt;&gt;&gt; Angela=Person('Angela',5)\r\n&gt;&gt;&gt; Nick&gt;Angela<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; Kim=Person('Kim',-8)\r\n&gt;&gt;&gt; abs(Kim)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">8<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; Tom=Person('Tom',7)\r\n&gt;&gt;&gt; Mikayla=Person('Mikayla',3)\r\n&gt;&gt;&gt; Tom+=Mikayla\r\n&gt;&gt;&gt; Tom<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">10<\/div>\n<p>To leave this lesson on an engaging note, we would just like to leave this code here:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; '1'.__add__('1')<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8217;11&#8217;<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; 1.__add__(1)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">SyntaxError: invalid syntax<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; [1,2,3].__add__([4,5,6])<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[1, 2, 3, 4, 5, 6]<\/div>\n<p>So, this was all about Python Operator overloading and Python Magic Method Tutorial. Hope you like it.<\/p>\n<h3>Interview Questions on Python Operator Overloading and Python Magic Methods<\/h3>\n<p>1. How do you overload an operator in Python?<\/p>\n<p>2. What are the magical methods in Python?<\/p>\n<p>3. Which operator is overloaded by invert in Python?<\/p>\n<p>4. How do you call a magic method in Python?<\/p>\n<p>5. Which operators cannot be overloaded?<\/p>\n<h3>Conclusion<\/h3>\n<p>Hence, we studied Python Operator overloading, in-effect, is pure syntactic sugar. Through it, we override a magic method to be able to use an operator on a custom class.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this Python tutorial, we are going to discuss Python Operator Overloading, examples of operator overloading in Python, and Python magic methods with some operators: Python binary operators, Python comparison operators, Python unary operators,&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":36203,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[1991,2778,8480,9285,10389,10434,10744,10906],"class_list":["post-7254","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-binary-operators-in-python","tag-comparison-operators-in-python","tag-magic-methods-in-python","tag-operator-overloading-in-python","tag-python-binary-operators","tag-python-comparison-operators","tag-python-operators","tag-python-unary-operators"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Operator Overloading and Python Magic Methods - DataFlair<\/title>\n<meta name=\"description\" content=\"Python Operator Overloading with examples - Python Magic Methods: Binary, Unary, comparison &amp; Extended Assignments python Operators\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/python-operator-overloading\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Operator Overloading and Python Magic Methods - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Python Operator Overloading with examples - Python Magic Methods: Binary, Unary, comparison &amp; Extended Assignments python Operators\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-operator-overloading\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-02-05T10:58:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-20T09:59:21+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Operator-Overloading-01.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Operator Overloading and Python Magic Methods - DataFlair","description":"Python Operator Overloading with examples - Python Magic Methods: Binary, Unary, comparison & Extended Assignments python Operators","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/python-operator-overloading\/","og_locale":"en_US","og_type":"article","og_title":"Python Operator Overloading and Python Magic Methods - DataFlair","og_description":"Python Operator Overloading with examples - Python Magic Methods: Binary, Unary, comparison & Extended Assignments python Operators","og_url":"https:\/\/data-flair.training\/blogs\/python-operator-overloading\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-02-05T10:58:09+00:00","article_modified_time":"2026-04-20T09:59:21+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Operator-Overloading-01.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-operator-overloading\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-operator-overloading\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Python Operator Overloading and Python Magic Methods","datePublished":"2018-02-05T10:58:09+00:00","dateModified":"2026-04-20T09:59:21+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-operator-overloading\/"},"wordCount":962,"commentCount":6,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-operator-overloading\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Operator-Overloading-01.jpg","keywords":["Binary Operators in Python","comparison operators in python","Magic Methods in Python","Operator Overloading in Python","Python Binary Operators","python comparison operators","python operators","Python Unary Operators"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-operator-overloading\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-operator-overloading\/","url":"https:\/\/data-flair.training\/blogs\/python-operator-overloading\/","name":"Python Operator Overloading and Python Magic Methods - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-operator-overloading\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-operator-overloading\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Operator-Overloading-01.jpg","datePublished":"2018-02-05T10:58:09+00:00","dateModified":"2026-04-20T09:59:21+00:00","description":"Python Operator Overloading with examples - Python Magic Methods: Binary, Unary, comparison & Extended Assignments python Operators","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-operator-overloading\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-operator-overloading\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/python-operator-overloading\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Operator-Overloading-01.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Operator-Overloading-01.jpg","width":1200,"height":628,"caption":"Python Operator Overloading and Python Magic Methods"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-operator-overloading\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python Operator Overloading and Python Magic Methods"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/7254","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=7254"}],"version-history":[{"count":16,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/7254\/revisions"}],"predecessor-version":[{"id":147711,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/7254\/revisions\/147711"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/36203"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=7254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=7254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=7254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}