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.

 Python has a unique technique written as “ init ()”. This technique is a constructor. So what does it express anyway? It means that whenever we produce an object of a class, the init () methodology gets called automatically and initializes our object. But with what values does it initialize it? Well, the values we prescribe inside the init () system are applied to initialize the data members.

 

 

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

Popular posts from this blog

Machine learning and Artificial Intelligence

Animations using AI

Stemming in Python