Default constructor and Parameterized Constructors difference
- Minu k
- Jun 6, 2022
- 2 min read

A constructor is a special function that gets called automatically when an object is created of a class. The main intent of a constructor is to initialize or entrust values to the data factors of that class. It can not restore any value other than none.
Constructors are always called when an object is created and is acted by the, init,() system. It accepts the self- keyword, which refers to itself( the object), as a first argument which allows entering the attributes or system of the class.
Rules of Python Constructor
It starts with the def keyword like all disparate functions in Python.
It's supervened by the word init which is prefixed and suffixed with dual underscores with a couple of classes i.e.,, init,().
It takes an argument called as self with which it assigns valuations to the variable.
Difference Between Default constructor and Parameterized Constructors
Default constructor
A default constructor is a 0 argument constructor which contains a no- argument call to the super class constructor.
To charge default values to the lately created objects is the main blame of default constructor.
Compiler writes a default constructor in the code only if the program doesn't scribble any constructor in the class.
The access modifier of default constructor is perpetually the identical as a class modifier but this rule is serviceable only for “ public ” and “ default ” modifiers.
Parameterized Constructors
The parameterized constructors are the constructors having a especial number of arguments to be handed over.
The goal of a parameterized constructor is to entrust user- wanted specific values to the case variables of distinct objects.
A parameterized constructor is written explicitly by a programmer.
The fit modifier of default constructor is ever the indistinguishable as a class modifier but this rule is functional only for “ public ” and “ default ” modifiers.
Conclusion
In this blog , we saw about constructor in python , rules for writing constructor, and Default constructor and Parameterized Constructors difference.
You can also visit constructor overloading in python here.
Comentários