How do you define a constructor in a class in Python?

Experience Level: Junior
Tags: Python

Answer

To define a constructor in a class, create a method with name __init__ and argument self.

class Person:
    def __init__(self):

        print("Instance of object was created")

Python for beginners
Python for beginners

Are you learning Python ? Try our test we designed to help you progress faster.

Test yourself