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")
Related Python job interview questions
Why would you use a private method in Python?
Python JuniorHow do you pass an argument to a class constructor in Python?
Python JuniorWhat is a convention to name a protected method in Python?
Python JuniorWhat is a convention to name a private method in Python?
Python JuniorHow do you communicate via code what argument types and what return value type a function has?
Python Junior