{"id":114,"date":"2026-03-02T08:47:16","date_gmt":"2026-03-02T03:47:16","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=114"},"modified":"2026-03-16T09:30:57","modified_gmt":"2026-03-16T04:30:57","slug":"magic-methods","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/magic-methods\/","title":{"rendered":"Magic Methods"},"content":{"rendered":"\n<p>Magic Methods are special methods in Python that start and end with double underscores.<\/p>\n\n\n\n<p>They are also called Dunder Methods (Double Under methods).<\/p>\n\n\n\n<p>Example:<br><code>__init__<\/code>, <code>__str__<\/code>, <code>__len__<\/code><\/p>\n\n\n\n<p>These methods are automatically called by Python in special situations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Why Magic Methods are Important<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Customize object behavior<\/li>\n\n\n\n<li>Enable operator overloading<\/li>\n\n\n\n<li>Control how objects behave with built-in functions<\/li>\n\n\n\n<li>Improve readability and flexibility<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. <code>__init__()<\/code> \u2013 Constructor<\/strong><\/h2>\n\n\n\n<p>Called automatically when an object is created.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Student:<br>    def __init__(self, name):<br>        self.name = names1 = Student(\"Hira\")<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. <code>__str__()<\/code> \u2013 String Representation<\/strong><\/h2>\n\n\n\n<p>Defines what is printed when you print the object.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Student:<br>    def __init__(self, name):<br>        self.name = name    def __str__(self):<br>        return f\"Student Name: {self.name}\"s1 = Student(\"Hira\")<br>print(s1)<\/pre>\n\n\n\n<p>Without <code>__str__()<\/code>, Python prints the memory location.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. <code>__len__()<\/code> \u2013 Length Function<\/strong><\/h2>\n\n\n\n<p>Allows object to work with <code>len()<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class MyList:<br>    def __init__(self, items):<br>        self.items = items    def __len__(self):<br>        return len(self.items)obj = MyList([1, 2, 3])<br>print(len(obj))<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. <code>__add__()<\/code> \u2013 Operator Overloading<\/strong><\/h2>\n\n\n\n<p>Allows objects to use <code>+<\/code> operator.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Number:<br>    def __init__(self, value):<br>        self.value = value    def __add__(self, other):<br>        return Number(self.value + other.value)n1 = Number(10)<br>n2 = Number(5)result = n1 + n2<br>print(result.value)<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. <code>__eq__()<\/code> \u2013 Equality Comparison<\/strong><\/h2>\n\n\n\n<p>Defines behavior for <code>==<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Student:<br>    def __init__(self, marks):<br>        self.marks = marks    def __eq__(self, other):<br>        return self.marks == other.markss1 = Student(90)<br>s2 = Student(90)print(s1 == s2)<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Commonly Used Magic Methods<\/strong><\/h2>\n\n\n\n<p><code>__init__()<\/code> \u2013 Constructor<br><code>__str__()<\/code> \u2013 String display<br><code>__repr__()<\/code> \u2013 Official representation<br><code>__len__()<\/code> \u2013 Length<br><code>__add__()<\/code> \u2013 Addition<br><code>__sub__()<\/code> \u2013 Subtraction<br><code>__mul__()<\/code> \u2013 Multiplication<br><code>__eq__()<\/code> \u2013 Equality check<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Key Points<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Always use double underscores before and after<\/li>\n\n\n\n<li>Automatically called by Python<\/li>\n\n\n\n<li>Used to modify built-in behavior<\/li>\n\n\n\n<li>Enable operator overloading<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Key Takeaway<\/strong><\/h2>\n\n\n\n<p>Magic Methods allow you to customize how objects behave with built-in Python operations.<\/p>\n\n\n\n<p>They make classes powerful, flexible, and fully integrated with Python\u2019s built-in features.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/python\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">OBJECT ORIENTED PROGRAMMING IN PYTHON (OOP) > Advanced OOP > Magic Methods<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773635415290\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":56,"template":"","class_list":["post-114","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Magic Methods - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn Python magic methods (dunder methods) like init, str, len, and operator overloading to customize object behavior.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/python\/lesson\/magic-methods\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Magic Methods - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn Python magic methods (dunder methods) like init, str, len, and operator overloading to customize object behavior.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/magic-methods\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-16T04:30:57+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/magic-methods\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/magic-methods\\\/\",\"name\":\"Magic Methods - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-02T03:47:16+00:00\",\"dateModified\":\"2026-03-16T04:30:57+00:00\",\"description\":\"Learn Python magic methods (dunder methods) like init, str, len, and operator overloading to customize object behavior.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/magic-methods\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/magic-methods\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/magic-methods\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"OBJECT ORIENTED PROGRAMMING IN PYTHON (OOP) > Advanced OOP > Magic Methods\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\",\"name\":\"One Language. Endless Possibilities\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/python\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Magic Methods - One Language. Endless Possibilities","description":"Learn Python magic methods (dunder methods) like init, str, len, and operator overloading to customize object behavior.","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:\/\/gigz.pk\/python\/lesson\/magic-methods\/","og_locale":"en_US","og_type":"article","og_title":"Magic Methods - One Language. Endless Possibilities","og_description":"Learn Python magic methods (dunder methods) like init, str, len, and operator overloading to customize object behavior.","og_url":"https:\/\/gigz.pk\/python\/lesson\/magic-methods\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-16T04:30:57+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/python\/lesson\/magic-methods\/","url":"https:\/\/gigz.pk\/python\/lesson\/magic-methods\/","name":"Magic Methods - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-02T03:47:16+00:00","dateModified":"2026-03-16T04:30:57+00:00","description":"Learn Python magic methods (dunder methods) like init, str, len, and operator overloading to customize object behavior.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/magic-methods\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/magic-methods\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/magic-methods\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"OBJECT ORIENTED PROGRAMMING IN PYTHON (OOP) > Advanced OOP > Magic Methods"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/python\/#website","url":"https:\/\/gigz.pk\/python\/","name":"One Language. Endless Possibilities","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson\/114","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/media?parent=114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}