Tutorial 9 : OOPs in Python — Magic methods or Dunder methods

Photo by Hitesh Choudhary on Unsplash

Missed previous lecture ?? — Click here

Feel free to navigate directly to any topic given below :

Using built-in functions for user-defined class objects

magic method __str__() for user-defined class objects

magic methods __int__() and __float__() for user-defined class objects

magic methods __add__() and __mul__() for user-defined class objects

magic methods __lt__() , __ge__() , __ne__() for user-defined class objects

Using built-in functions for user-defined class objects

We cannot use built-in functions for user-defined objects. So we use magic method solve this issue.

Magic methods are preceded and followed by two underscores.

magic method __str__() for user-defined class objects

magic methods __int__() and __float__() for user-defined class objects

magic methods __add__() and __mul__() for user-defined class objects

magic methods __lt__() , __ge__() , __ne__() for user-defined class objects

Next tutorial is about more of such Magic methods with many examples.

So we come to the conclusion that, in order to make Python’s built-in functions to work for user-defined class objects we need to make use of magic methods.

--

--

--

I am a keen learner and diligent teacher with special interest in mathematics and machine learning.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Python regular expression match, search and findall

@oropocket Another #UniFarm update is here!

Summer Reading List: 10 Software Development Books

How to manage your companies for free with ERPNext

CS371p Week 11: David Durairaj

My Journy in Dlithe Bootcamp(Week 5)

Groovy to create Build Pipeline which then deploys and tests the app on Kubernetes Cluster

The Urbach Letter 5/28/19

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gaurav Patil

Gaurav Patil

I am a keen learner and diligent teacher with special interest in mathematics and machine learning.

More from Medium

Tutorial 3 : OOPs in Python — Inheritance

The Command Line Interface for Beginners

Is your pen an object in Python ?

Numpy — slicing