Python Constructors and Types
Constructors in Python
Constructors are commonly useful in expressing an object. When we induce an object also constructors possess the task to initialize to the data members of the class.
Now that we decide what we mean by expressing, let us see what a
constructor looks like and how it achieves the task of initialization.
Syntax for creating constructor in Python
class ClassName
def, init, ( self)
# Method body
Illustration
class Mumbai
def, init, ( self)
print (" Object Created for the Class Mumbai")
obj1 = Mumbai ()
Output
Object Created for the Class Mumbai
Types of constructors
Parameterized constructor
We know that init () is a python technique. And techniques can possess
arguments. Hence init () can or can not have arguments. The init () approach
without arguments is known as a default constructor and when you give it with
parameters, also it's called a parameterized constructor.
Default constructor
This is a simple constructor that does n’t accept any arguments. It has
only one argument which has a authority to the example being constructed.
Conclusion
In this blog , we learned about Constructors in Python with an example, which we used to initialized the object ,types of constructor, parameterized
constructor and default constructor.
To learn about python multiple
constructors you can check this Blog.
Comments
Post a Comment