Make Python Objects Magical

Make Python Objects Magical

The very first thing one must learn and believe without a shred of doubt is that In python, everything is an object. Python offers a host of magic methods (alternatively called dunder methods because of the “double underscores”) to make custom objects more intuitive, easy to use and pythonic. Let’s start with a very basic scenario:

The very first thing one must learn and believe without a shred of doubt is that In python, everything is an object.

Python offers a host of magic methods (alternatively called dunder methods because of the “double underscores_”) to make custom objects more intuitive, easy to use and **_pythonic. **Let’s start with a very basic scenario:

string_sum_output = 'hello' + ' world' + '!'
integer_sum_output = 10 + 20
print(string_sum_output)
print(integer_sum_output)
o/p1:>> hello world!
o/p2:>> 30

At first glance o/p1 and o/p2 seems super normal. But let me ask you this,

how does python decide when to do arithmetic sum and when to concatenate values**if ‘+’ operator is used**?

Most obvious answer will be based on data-types, which is not incorrect, but doesn’t reveal the real process of deciding though. That’s where magic methods come into picture. Certain behaviours of an objects can be controled and dictated by using these, so called, magic methods.

So in this article, we will see _**_how to add certain behavioural traits to our custom classes**, so that objects of those classes will have some special functionalities.


Student Class

Following is our simple Student class:

A Basic Student Class

A very basic class, which takes three parameters to define a student — namephone_number and standard.


Scenarios

There are numerous magic methods available in python, we will see some basic and most widely used of those, to implement following scenarios:

  1. What will be shown, when I print an object?
  2. How can I compare between two objects?
  3. _What will be returned if I apply built-in _**_length_**_ function on one object?_
  4. How to make the object iterable/iterator?
  5. _How to support indexing on the object, i.e. _**_obj[0]_**?
  6. And finally, what will be the result if any arithmetic operator (‘+’, ‘-’, ‘*’ etc) is used with the objects?

python oop-concepts

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Python OOPS Concepts | Python OOP Tutorial | Python Classes and Objects

Learn Python OOPS. We'll understand the different object-oriented programming concepts and how Python implements them by creating classes and objects. You will get an idea about inheritance, encapsulation, and polymorphism and perform a hands-on demo on these concepts.

OOP in Python | Python OOP Concepts | Object Oriented Programming Python

OOP in Python will help you understand how to write various basic programs in python and real-time uses of some of the Python libraries as well.

Python OOP — Inheritance

Inheritance is a key concept in Object-Oriented Programming. It enables us to create a new class from an existing class. The new class is a specialized version of the existing class and it inherits all the non-private variables and methods of the existing class. When should we use inheritance? The answer is that whenever we come across an IS A relationship between objects.

Python Tricks Every Developer Should Know

In this tutorial, you’re going to learn a variety of Python tricks that you can use to write your Python code in a more readable and efficient way like a pro.

How to Remove all Duplicate Files on your Drive via Python

Today you're going to learn how to use Python programming in a way that can ultimately save a lot of space on your drive by removing all the duplicates. We gonna use Python OS remove( ) method to remove the duplicates on our drive. Well, that's simple you just call remove ( ) with a parameter of the name of the file you wanna remove done.