Introduction
Welcome to my 58th blog post on the Python journey. On day 58, I learned about a special method in a class that is used to create and initialize an object of a class. It is called Constructors. Let's dive into more details and understand these higher-order functions in Python.
So let's get started......
Constructors
It is a special method that is used to create and initialize an object of a class. There are different types of constructors.
When an object of a class is created, Constructor is invoked automatically.
The main purpose of a constructor is to initialize or assign values to the data members of that class.
It cannot return any value other than None.
Syntax Constructor -
def __init__(self):
# initializations
''init'' is one of the reserved functions in Python. In Object Oriented Programming, it is known as a constructor.
Types of Constructors in Python
Parameterized Constructor
Default Constructor
Parameterized Constructor in Python
When the constructor accepts arguments along with self, it is known as parameterized constructor.
The values can be assigned to the data members, by using the arguments inside the class.
class Details:
def __init__(self, animal, group):
self.animal = animal
self.group = group
obj1 = Details("Crab", "Crustaceans")
print(obj1.animal, "belongs to the", obj1.group, "group.")
Crab belongs to the Crustaceans group.
Default Constructor in Python
When the constructor doesn't accept any arguments from the object and has only one argument, ''self'', in the constructor, it is known as a Default constructor.
Example -
class Details:
def __init__(self):
print("animal Crab belongs to Crustaceans group")
obj1=Details()
Output -
animal Crab belongs to Crustaceans group
Resources Used
You can watch the video of Day#58 by clicking on the below link ๐๐๐๐๐
Conclusion
Thanks, guys for going through this blog post. On day 58, I learned about the special method in a class called Constructors in Python. I also learned about two types of constructors viz Parameterised and Default constructors.
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 #58
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.