Day #73 - Magic/Dunder Methods in Python

Day #73 - Magic/Dunder Methods in Python

Introduction

Welcome to my 73rd blog post on the Python coding journey. On day 73, I learned about Magic/Dunder methods in Python. These methods can be defined in classes, and when invoked, they can give you a powerful way to manipulate objects and their behavior.' Let's dive into more details

So let's get started......

Magic/Dunder Methods

  • These Methods are defined using double underscore surrounding their names.

  • They are used to implement special methods such as the addition, subtraction and comparison operators, as well as some more advanced techniques like descriptors and properties.

  • They are used to customize class behavior.

Let’s take a look at some of the most commonly used magic methods in Python.

init Method

  1. It is a special method that is invoked automatically when we create a new instance of a class.

  2. This method is responsible for setting up the initial state of an object, and it is where we would typically define any instance variables that we need. This method is called as 'constructor'

str and repr methods

  1. These methods are used to convert an object to a string representation.

  2. The 'str 'method is used when you want to print out an object, while the 'repr' method is used when you want to get a string representation of an object that can be used to recreate the object.

len method

  1. This method is used to get the length of an object.

  2. Using __len__ method we can find the size of a data structure, such as a list or dictionary.

call method

  1. The call method makes an object callable, meaning it can be passed as a parameter to a function and it will be executed when the function is called.

  2. This makes it an incredibly powerful tool that allows us to create objects that behave like functions.

Similarly, many other magic methods are available in Python, that allow us to customize the behavior of class and make code clean and easy to understand.

Example -

Here let's create 2 files. One is the class employee file, where we created the whole new class and next is the main file from where we make a call to the magic methods in the new class file employee.

emp.py

class Employee:

  def __init__(self, name):
    self.name = name

  def __len__(self):
    i = 0
    for c in self.name:
      i = i + 1
    return i

  def __str__(self):
    return f"The name of the employee is {self.name} str"

  def __repr__(self):
    return f"Employee('{self.name}')"

  def __call__(self):
    print("Hey I am good")

main.py

from emp import Employee

e = Employee("Harry")
print(str(e))
print(repr(e))
# print(e.name)
# print(len(e))
e()

Output

The name of the employee is Harry str
Employee('Harry')
Hey I am good

Resources Used

You can watch the video of Day#73 by clicking on the below link 👇👇👇👇👇

Conclusion

Thanks, guys for going through this blog post. On day 73, I learned about the magic methods and how they can be used to customize the behavior of the class. We also learned about the commonly used dunder method and practiced in the practical example. So using Magic methods we can make our code much cleaner and easier to understand.

Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of Python coding. This is it for Day #73

See you in the next one.....


About Me

Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.

I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.

So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.