{"id":32395,"date":"2022-07-30T23:13:37","date_gmt":"2022-07-30T23:13:37","guid":{"rendered":"https:\/\/www.askpython.com\/?p=32395"},"modified":"2026-09-29T17:26:12","modified_gmt":"2026-09-29T17:26:12","slug":"magic-methods","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/oops\/magic-methods","title":{"rendered":"The Magic Methods in Python"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The magic methods in Python programming language are specifically for Object Oriented Design. Every class that we create has its own magic methods. Python&#8217;s standard interpreter assigns these to every class we create inside it. So, in this article, we shall see in detail how to call and use magic methods for a better programming approach. Let the coding fun begin!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Brushing up OOP knowledge<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before we get to the main topic, let us understand and polish the knowledge of OOP concepts. We shall see only the basics. So, Object Oriented Programming is a way of enclosing the data members and member functions into a user-defined entity called a <strong>Class<\/strong>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Class is something that holds particular data items that relate to each other and communicate in a specific way. We access the properties and the member functions using <strong>Object<\/strong>. The object is an instance of a class. In any programming language, memory is never allocated when we create a class, but it is actually created when we create its instance I.e object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><b>The animal<\/b> is a type of class. In that, we include all the living beings that reside on Earth. So, everyone has their own way of living, food, and shelter. The animal just defines the blueprint of all these. For example, <strong>the cat is the object of the Animal class. It has four legs, eats mice, and lives in houses or bushes.<\/strong> In the same way, the tiger has four legs but it kills and eats many animals so we say that the tiger eats meat, it lives in the forest.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code Example with Python:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass Animal:\n    def __init__(self, legs, food, shelter):\n        self.legs = legs\n        self.food = food\n        self.shelter = shelter\n        \n    def showAnimal(self):\n        print(&quot;The animal has {} legs: &quot;.format(self.legs))\n        print(&quot;The animal eats: {}&quot;.format(self.food))\n        print(&quot;The animal lives in: {}&quot;.format(self.shelter))\n        \ncat = Animal(4, &quot;Mouse&quot;, &quot;House&quot;)\ntiger = Animal(4, &quot;Meat&quot;, &quot;Forest&quot;)\ncat.showAnimal()\ntiger.showAnimal()\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nThe animal has 4 legs: \nThe animal eats: Mouse\nThe animal lives in: House\nThe animal has 4 legs: \nThe animal eats: Meat\nThe animal lives in: Forest\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Explanation:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>The animal class contains the <strong>number of legs, food, and shelter as properties.<\/strong><\/li><li><strong>When we create an instance and insert the values inside the constructor, the difference in their behaviour<\/strong> is clear.<\/li><li><strong>So, objects of the same class can be different according to the behaviour of values.<\/strong><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">The magic methods in OOP<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">So, in the above example, we have a class as Animal. Python has a set of methods namely <strong>Dunder methods<\/strong> that are responsible for holding the properties, data members, and member functions of a class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>Definition: When we create an object, the Python interpreter calls special functions in the backend of code execution. They are known as Magic Methods or Dunder Methods.<\/em><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Why do we say Dunder? Because their names lie between <strong>double underscores<\/strong>. They perform some set of calculations that are just like magic while we create an object of a class. So, how do we check what are they and how many of them are there in the standard class? Use the steps below to find them:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>Create a sample class.<\/strong><\/li><li><strong>Create its object.<\/strong><\/li><li>Use the <strong>dir()<\/strong> function and insert the object inside it.<\/li><li><strong>This function prints a list of all the Magic Methods along with data members and member functions that are assigned to the class.<\/strong><\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(dir(cat))\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n__class__\n__delattr__\n__dict__\n__dir__\n__doc__\n__eq__\n__format__\n__ge__\n__getattribute__\n__gt__\n__hash__\n__init__\n__init_subclass__\n__le__\n__lt__\n__module__\n__ne__\n__new__\n__reduce__\n__reduce_ex__\n__repr__\n__setattr__\n__sizeof__\n__str__\n__subclasshook__\n__weakref__\nfood\nlegs\nshelter\nshowAnimal\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>The names that you see in double underscores are all magic methods. <\/strong>The rest of the properties are defined by the user. As we can see <code>__init__()<\/code> which is the constructor of any class in Python, is also a magic method. Let us see their use one by one. <strong>To understand their functionality always try to override the functions.<\/strong><\/p>\n\n\n\n<p class=\"has-ast-global-color-8-color has-white-background-color has-text-color has-background wp-block-paragraph\"><strong> One thing to note is that, for any class the user defines, there is some set of default magic methods with respect to each of them. <\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Use and implementation of some magic methods<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In this section, we shall see the use and implementation and use of some magic methods for writing a better <a href=\"https:\/\/www.askpython.com\/python\/oops\/object-oriented-programming-python\" data-type=\"post\" data-id=\"798\">OOP design<\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. __new__():<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This method helps the constructor __init__() method to create objects for a class. So, when we create an instance of a class, the Python interpreter first calls the __new__() method and after that__init__() method. They work hand in hand with each other. <\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>When a programmer opts to create an object, __new__() gets invoked that accepts the name of the object.<\/li><li>Then __init__() is invoked where the parameters including <strong>self<\/strong> are inserted into the object which in turn helps us to modify the class properties.<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass Sample:\n    def __new__(self, parameter):\n        print(&quot;new invoked&quot;, parameter)\n        return super().__new__(self)\n        \n    def __init__(self, parameter):\n        print(&quot;init invoked&quot;, parameter)\n        \nobj = Sample(&quot;a&quot;)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nnew invoked a\ninit invoked a\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Explanation:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>First, we create a class as Sample.<\/li><li>Then override the __new__() method by creating it. Then, as usual, the self parameter comes, and after that gives a simple parameter.<\/li><li>Return a super() function with the <code>__new__()<\/code> function with the self parameter to get access to the customizations we make to the method.<\/li><li>Then, with the same practice call the <code>__init__()<\/code> function with some parameter.<\/li><li>After that create an object of a sample class.<\/li><li><strong>Now, when we run the code, the interpreter first calls the __new__(), and then it calls __init__() method. <\/strong><\/li><\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">2. __init__():<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Python is an <a href=\"https:\/\/www.askpython.com\/python\/oops\/object-oriented-programming-python\" data-type=\"post\" data-id=\"798\">Object Oriented Programming<\/a> language. So, the class must have a constructor. This requirement is fulfilled using the __init__() method. When we create a class and want to give some initial parameters to it. The initializer method performs this task for us. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass Sample:        \n    def __init__(self, parameter):\n        print(&quot;init invoked&quot;, parameter)\n\nobj = Sample(&quot;a&quot;)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ninit invoked a\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Explanation:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Create\/override the __init__() method. Insert self parameter to signal the interpreter that this is a class method.<\/li><li>Insert required parameter.<\/li><li>Then print that parameter using the print() function.<\/li><li>After that create an object.<\/li><li>When we run the code we get the output as &#8220;init invoked a&#8221;, this states that <strong>the interpreter calls init() and prints that parameter.<\/strong><\/li><\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">3. __str__():<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This method helps us to display the object according to our requirements. So, let us say that when we create an object and try to print it. The print() function displays the memory location of the object. If we want to modify we can do this. <strong>The __str__() function gives a nice representation of the object.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code (before using __str__()):<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass Student:\n    def __init__(self, name, roll_no):\n        self.name = name\n        self.roll_no = roll_no\n        \nstud_1 = Student(&quot;Suresh&quot;, 1)\nprint(stud_1) \n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&lt;__main__.Student object at 0x0000023E2CF37CA0&gt;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Code (after using __str__()):<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass Student:\n    def __init__(self, name, roll_no):\n        self.name = name\n        self.roll_no = roll_no\n        \n    def __str__(self):\n        return (&quot;{} {}&quot;.format(self.name, self.roll_no))\n        \nstud_1 = Student(&quot;Suresh&quot;, 1)\nprint(stud_1) \n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nSuresh 1\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Cool right! now we can also use similar methods. We can format the object as according to our needs.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">4. __repr__():<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Similar to the __str__(), we can use __repr__ function for the decoration of objects. The code is similar to __str__() implementation.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass Student:\n    def __init__(self, name, roll_no):\n        self.name = name\n        self.roll_no = roll_no\n        \n    def __repr__(self):\n        print(&quot;repr invoked&quot;)\n        return (&quot;{} {}&quot;.format(self.name, self.roll_no))\n        \nstud_1 = Student(&quot;Suresh&quot;, 1)\nprint(stud_1) \n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nrepr invoked\nSuresh 1\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\">5. __sizeof__():<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">When we create a class the interpreter never assigns memory to it. It assigns memory to the object. If we want to know the memory allocated to that object, then we can call or override the __sizeof__() function and pass our object. This also returns size of a list =, tuple, dictionary object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass Student:\n    def __init__(self, name, roll_no):\n        self.name = name\n        self.roll_no = roll_no\n        \nstud_1 = Student(&quot;Suresh&quot;, 1)\nprint(&quot;Size of student class object: &quot;, stud_1.__sizeof__()) \n\nlist_1 = &#x5B;1, 2, 3, 4]\ntup_1 = (1, 2, 3, 4, 5)\ndict_1 = {&quot;a&quot;:1, &quot;b&quot;:2, &quot;c&quot;:3, &quot;d&quot;:4}\nprint(&quot;Size of list: &quot;, list_1.__sizeof__())\nprint(&quot;Size of tuple: &quot;, tup_1.__sizeof__())\nprint(&quot;Size of dictionary: &quot;, dict_1.__sizeof__())\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nSize of student class object:  32\nSize of list object:  104\nSize of tuple object:  64\nSize of dictionary object:  216\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\">6. __add__():<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This magic method is specifically similar to its name. It <a href=\"https:\/\/www.askpython.com\/python\/examples\/addition-in-python\" data-type=\"post\" data-id=\"19100\">adds two variables<\/a>. For an integer it returns the sum, for a string, it returns their concatenation result.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass Numbers:\n    def __init__(self, a, b):\n        self.a = a\n        self.b = b\n        \n    def __add__(self):\n        print(&quot;__add__ invoked&quot;)\n        return self.a + self.b\n\nnum = Numbers(3, 4)\nnum_2 = Numbers(&quot;a&quot;, &quot;b&quot;)\nprint(num.__add__())\nprint(num_2.__add__())\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n__add__ invoked\n7\n__add__ invoked\nab\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\">7. __reduce__():<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This magic method returns a set or a dictionary of all the parameters of a class and their values in <strong>key: value<\/strong> format. This can be directly called using the object name with the dot operator. So, when we <a href=\"https:\/\/www.askpython.com\/python\/oops\/python-classes-objects\" data-type=\"post\" data-id=\"712\">create a class<\/a> and instantiate it with some values. The function shall return it with the name of the parameters which were given during the declaration of a class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass Employee:\n    def __init__(self, name, salary):\n        self.name = name\n        self.sal = salary\n        \nemp = Employee(&quot;Shrinivas&quot;, 150000)\nprint(emp.__reduce__())\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n(&amp;lt;function _reconstructor at 0x0000023E22892EE0&gt;, (&amp;lt;class &#039;__main__.Employee&#039;&gt;, &amp;lt;class &#039;object&#039;&gt;, None), {&#039;name&#039;: &#039;Shrinivas&#039;, &#039;sal&#039;: 150000})\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Code (after overriding __reduce__()):<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass Employee:\n    def __init__(self, name, salary):\n        self.name = name\n        self.sal = salary\n        \n    def __reduce__(self):\n        return self.name, self.sal\n        \nemp = Employee(&quot;Shrinivas&quot;, 150000)\nprint(emp.__reduce__())\n\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n{&quot;Shrinivas&quot;, 150000}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Explanation:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When we override and try to return the parameters, we only get their values in a set.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">8. __hash__():<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The __hash__() function returns a specific hash value of the object stored in the <a href=\"https:\/\/www.askpython.com\/python\/examples\/min-heap\" data-type=\"post\" data-id=\"13589\">heap memory<\/a>. We can either override it or call it using the object name. <a href=\"https:\/\/www.askpython.com\/python-modules\/oshash-module\" data-type=\"post\" data-id=\"24926\">Hashing<\/a> is very useful to fetch the memory address of any random element in a computer. All programming languages use hash for the sake of simplicity and for memory allocation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass Employee:\n    def __init__(self, name, salary):\n        self.name = name\n        self.sal = salary\n    \n    def __hash__(self):\n        return super().__hash__()\n        \nemp = Employee(&quot;Shrinivas&quot;, 150000)\nprint(emp.__hash__())\n\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n154129100057\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Code:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass Employee:\n    def __init__(self, name, salary):\n        self.name = name\n        self.sal = salary\n    \nemp = Employee(&quot;Shrinivas&quot;, 150000)\nprint(emp.__hash__())\n\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n154129054082\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\">9. __getattribute__(name):<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This function returns the value of the attribute of a class if it exists. We need to call the function and pass the attribute which we assigned to the class parameter using the <strong>self <\/strong>keyword. Like if we assign the value of <strong>salary <\/strong>to <strong>self.sal <\/strong>we need to call <strong>sal<\/strong> inside the __getattribute__() function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass Employee:\n    def __init__(self, name, salary):\n        self.name = name\n        self.sal = salary\n        \n    def __getattribute__(self, name):\n        return super().__getattribute__(name)\n        \nemp = Employee(&quot;Ravi&quot;, 500000)\nprint(emp.__getattribute__(&quot;sal&quot;))\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n50000\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Explanation:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this function, the <strong>&#8220;self.sal&#8221; <\/strong>is assigned to the <strong>salary <\/strong>parameter of the Employee class. The function returns its value as the attribute that exists inside the class. If it does not exist the function returns an error message.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">10. __setattr__(name, value):<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">As the name suggests, this magic method helps us change the value of an attribute when we define the object. No need to override the <strong>__getattribute__() and __setattr__()<\/strong> functions. Just call them using the objects created.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass Employee:\n    def __init__(self, name, salary):\n        self.name = name\n        self.sal = salary\n\n        \nemp = Employee(&quot;Ravi&quot;, 500000)\nemp.__setattr__(&quot;name&quot;, &quot;Suresh&quot;)\nemp.__setattr__(&quot;sal&quot;:600000)\nprint(&quot;The changed name of the employee is: &quot;, emp.__getattribute__(&quot;name&quot;))\nprint(&quot;The changed salary of the employee is: &quot;, emp.__getattribute__(&quot;sal&quot;))\n\n        \n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nThe changed name of the employee is: Suresh\nThe changed salary of the employee is: 600000\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Explanation:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>the __setattr__() take two parameters. <\/strong><ol><li><strong>name of attribute<\/strong><\/li><li><strong>its new value<\/strong><\/li><\/ol><\/li><li><strong>Then it assigns that particular value to that attribute.<\/strong><\/li><li><strong>After that, to check the value assigned to it call the __getattrbute__() function using the employee object and dot operator. emp.__getattribute(&#8220;name&#8221;).<\/strong><\/li><\/ol>\n\n\n\n<p class=\"has-ast-global-color-8-color has-white-background-color has-text-color has-background wp-block-paragraph\"><strong>Point to note: These two functions replace getter and setter methods for a class in Python.<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">So, we saw the in-depth implementation of some of the magic methods in Python. I hope this helps and will make programming easier. They prove to be helpful pieces of code for quick implementation and use. Happy python programming &#x1f40d;&#x1f40d;&#x1f60e;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The magic methods in Python programming language are specifically for Object Oriented Design. Every class that we create has its own magic methods. Python&#8217;s standard interpreter assigns these to every class we create inside it. So, in this article, we shall see in detail how to call and use magic methods for a better programming [&hellip;]<\/p>\n","protected":false},"author":36,"featured_media":69541,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-32395","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oops"],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/32395","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/36"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=32395"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/32395\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/69541"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=32395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=32395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=32395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}