What is magic or dunder method in OOPs in python

This recipe explains what is magic or dunder method in OOPs in python

Recipe Objective

This recipe explains what is magic or dunder method.

Magic Or Dunder Method

Dunder is derived from “Double Under (Underscores)”. Magic or dunder method are special methods in python which have double suffix and double prefix underscore in method name these methods are used in function overloading. Dunder methods can be used to match the behavior of built-in types to user-defined objects.

Explore the Real-World Applications of Recommender Systems

# Magic or Dunder Methods in Python
class Complex:
    def __init__(self, real, imag): # constructor
        self.real = real
        self.imag = imag
    
    def __add__(self, other): # dunder method
        return Complex(self.real + other.real, self.imag + other.imag)
    
    def show(self): #class method
        print(self.real, "+", self.imag, "i")

x = Complex(3, 7)
y = Complex(-4, 11)
z = x + y

z.show()

 

What Users are saying..

profile image

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Skip Gram Model Python Implementation for Word Embeddings
Skip-Gram Model word2vec Example -Learn how to implement the skip gram algorithm in NLP for word embeddings on a set of documents.

Recommender System Machine Learning Project for Beginners-4
Collaborative Filtering Recommender System Project - Comparison of different model based and memory based methods to build recommendation system using collaborative filtering.

Digit Recognition using CNN for MNIST Dataset in Python
In this deep learning project, you will build a convolutional neural network using MNIST dataset for handwritten digit recognition.

Build Classification Algorithms for Digital Transformation[Banking]
Implement a machine learning approach using various classification techniques in Python to examine the digitalisation process of bank customers.

Linear Regression Model Project in Python for Beginners Part 1
Machine Learning Linear Regression Project in Python to build a simple linear regression model and master the fundamentals of regression for beginners.

Classification Projects on Machine Learning for Beginners - 1
Classification ML Project for Beginners - A Hands-On Approach to Implementing Different Types of Classification Algorithms in Machine Learning for Predictive Modelling

Time Series Classification Project for Elevator Failure Prediction
In this Time Series Project, you will predict the failure of elevators using IoT sensor data as a time series classification machine learning problem.

Learn to Build Generative Models Using PyTorch Autoencoders
In this deep learning project, you will learn how to build a Generative Model using Autoencoders in PyTorch

Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.