What is an example of a constructor in Python?

Here’s an example of a constructor in Python:

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def introduce(self):
        print(f"Hi, my name is {self.name} and I'm {self.age} years old.")

# Creating an instance of the Person class
person = Person("John", 25)

# Calling the introduce method
person.introduce()
Code language: Python (python)

In the example above, __init__ is the constructor method of the Person class. It is called automatically when you create a new instance of the class. The constructor takes in the self parameter (which refers to the instance being created) along with additional parameters like name and age. Inside the constructor, the values passed as arguments are assigned to instance variables (self.name and self.age).

You can then create an instance of the Person class using the constructor, passing the necessary arguments. In this case, person = Person("John", 25) creates a new Person object with the name “John” and age 25.

Finally, the introduce method is called on the person instance, which prints a simple introduction using the instance variables. Running this code would output: “Hi, my name is John and I’m 25 years old.”

What is constructor in Python?

In Python, a constructor is a special method that is automatically called when an object (instance) of a class is created. It is used to initialize the attributes (variables) of the object and perform any necessary setup or initialization tasks.

The constructor method is named __init__ and it is defined within a class. It takes at least one parameter, typically named self, which refers to the instance being created. Additional parameters can be added to the constructor to accept values that are used to initialize the attributes of the object.

Here’s a summary of key points about constructors in Python:

  • The constructor method is defined using the __init__ name.
  • It is automatically called when an object of the class is created.
  • The first parameter of the constructor is always self, which refers to the instance being created.
  • Additional parameters can be added to the constructor to initialize the attributes of the object.
  • Inside the constructor, you can assign values to instance variables using the self keyword.
  • The constructor can also include other initialization logic or perform any other necessary setup tasks.

Constructors are useful for ensuring that an object is properly initialized when it is created, and for setting up any initial state or configurations that are required by the object.

Read More;

  • I am an aspiring software engineer currently studying at the Islamic University of Technology in Bangladesh. My technical skills include Python automation, data science, machine learning, PHP, cURL and more. With a passion for creating innovative solutions, I am dedicated to mastering the art of software engineering and contributing to the technological advancements of tomorrow.

    View all posts

Leave a Comment