1.

What is __init__?

Answer»

__init__ is a contructor method in Python and is automatically called to allocate memory when a new object/instance is created. All classes have a __init__ method associated with them. It helps in distinguishing METHODS and attributes of a class from LOCAL variables.

# class definitionclass Student: def __init__(self, fname, lname, age, section): self.firstname = fname self.lastname = lname self.age = age self.section = section# creating a new objectstu1 = Student("SARA", "Ansh", 22, "A2")


Discussion

No Comment Found