Prerequisites Crushed
First of all, everything in Python is an object.
>>Understanding Datatype in Python
let us understand very basic datatype in Python i.e., int.
num = 10
# Printing the type of integer variable num
print(type(num))
#--------------------output----------------------
#<class 'int'>
So, here you can see the type of integer variable num is class 'int', This means num is an object of class int.
Result: Datatype is nothing but class.
Application: To create our own datatype we need to create a class.
>> Creating a DataType for Complex Numbers.
A complex number looks like ๐, where x and y can be any real number and i (iota) is sqrt(-1). For more info
$$x+iy$$
class ComplexNumber :
# writing a constructor for class to initialize value of x and y
def __init__(self,x,y):
self.x_coordinate = x
self.y_coordinate = y
#creating a complex no. from our class(datatype)
num1 = ComplexNumber(2,3)
#Printing our new complex number num1
print(num1)
#------------------------OUTPUT-------------------------------
# <cpn.ComplexNumber object at 0x7ff2a00a5cf0>
Oops! It must have looked like 2+i3
, but it is showing some weird thing.
>> How to change its appearance while printing
There is something like magic methods in Python.
Constructor(__init__
) for a class is an example of a magic method, basically functions like __MethodName__ are magic methods. These are pre-built in Python.
These magic methods are called only when the criteria which is pre-set satisfies. for example, the constructor __init__
gets executed as we create an object of the class.
__str__
is one of these magic methods which helps us in defining how our new datatype's object should look like when it is printed.
#Updating Class ComplexNumber
class ComplexNumber :
# writing a constructor for class to initialize value of x and y
def __init__(self,x,y):
self.x_coordinate = x
self.y_coordinate = y
def __str__(self):
return f"{self.x_coordinate} + i{self.y_coordinate}"
#creating a complex no. from our class(datatype)
num1 = ComplexNumber(2,3)
#Printing our new complex number num1
print(num1)
#------------------------OUTPUT-------------------------------
# 2 + i3
we can build an addition behavior in our class( datatype)
For that, we have __add__
magic method in our inventory.
#Updating Class ComplexNumber
class ComplexNumber :
# writing a constructor for class to initialize value of x and y
def __init__(self,x,y):
self.x_coordinate = x
self.y_coordinate = y
# defining how to print object of our class(datatype)
def __str__(self):
return f"{self.x_coordinate} + i{self.y_coordinate}"
# Building addition functionality to our datatype
def __add__(self,other):
return ComplexNumber(
self.x_coordinate + other.x_coordinate,
self.y_coordinate + other.y_coordinate)
from cpn import ComplexNumber
#creating a complex no. from our class(datatype)
num1 = ComplexNumber(2,3)
#Printing our new complex number num1
print(num1)
#------------------------OUTPUT-------------------------------
# 2 + i3
num2 = ComplexNumber(3,2)
num3= num1 + num2
print(num3)
#------------------------OUTPUT--------------------------------
#5 + i5
Conclusion
In this article, I have demonstrated the easiest example of how we can use OOPs in Python to create our own datatype.
And we can use this power to improve the efficiency of our solution while solving real-world problems.
There are lots of magic methods that you can explore and build some amazing stuff with it.
This is my first ever blog so share your thoughts with me on socials if you like it and correct me if I did a mistake somewhere.
Thanks for reading.