Categories: python

Constructor in Python

What is a Constructor?

  • A constructor is a special method (function) used to initialize the class’s instance members.
  • The main goal of constructors is to assign values to the class’s data members when an object of the class is created.
  • Constructors are always called when an object is created, and the init() method simulates this. It accepts the self-keyword, which refers to itself (the object), as the first argument, allowing access to the class’s attributes or methods.

Syntax:

class class_name:
   def __init__(self):
      # body of the constructor

If you then create an example, the constructor will be called, i.e,

object_1= class_name()

Example:

class Student:

 # default constructor
 def __init__(self):
  self.name = "Rohan Sharma"

 # a method for printing data members
 def print_name(self):
  print(self.name)


# creating object of the class
obj = Student()

# calling the instance method using the object obj
obj.print_name()

Output:

Rohan Sharma

By writing Student() in the code above, we are informing python that obj is an object of class Student. And that is exactly when the constructor of that class is called.

More than One Constructor in a Single class:

The object of the class will always call the last constructor if the class has multiple constructors. For instance,

class Student:  
    def __init__(self):  
        print("The First Constructor")  
    def __init__(self):  
        print("The second contructor")  
  
st = Student()  

Output:

The second contructor
Built-in class attributes:

There are some built-in class attributes that provide information about the class.

AttributeDescription
__dict__The dictionary containing details about the class namespace is made available.
__doc__It contains a string that has the class documentation
__name__It is used to access the class name.
__module__It is used to access the module in which, this class is defined.
__bases__It contains a tuple including all base classes.

Note: also read about Python Class

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Recent Posts

Staircase Climbing Ways

There is a staircase with N steps, and you can ascend either 1 step or…

5 days ago

Autocomplete System Implementation

Build an autocomplete system that, given a query string s and a set of possible…

5 days ago

Job Scheduler Implementation

Design a job scheduler that accepts a function f and an integer n. The scheduler…

6 days ago

Largest Sum of Non-Adjacent Numbers

Problem Statement (Asked By Airbnb) Given a list of integers, write a function to compute…

1 week ago

Count Unival Subtrees in a Binary Tree

A unival tree (short for "universal value tree") is a tree in which all nodes…

1 week ago

Count Decoding Ways for Encoded Messages

Problem Statement (Asked By Facebook) Given the mapping a = 1, b = 2, ...,…

1 week ago