+1 vote
in Python by
What is __init__ in Python?

2 Answers

0 votes
by

_init_ methodology is a reserved method in Python aka constructor in OOP. When an object is created from a class and _init_ methodolgy is called to acess the class attributes.

0 votes
by

In Python,__init__ is a method or constructor. It is automatically called to allocate memory when a new object or instance of a class is created. All classes have the __init__ method.

Here’s how to use the __init__ method in Python:

# class definition

class Student:

    def __init__(self, fname, lname, age, section):

        self.firstname = fname

        self.lastname = lname

        self.age = age

        self.section = section

# creating a new object

stu1 = Student(“Sara”, “Ansh”, 22, “A2”)

Related questions

+1 vote
asked Feb 15, 2021 in Python by SakshiSharma
+1 vote
asked Feb 13, 2021 in Python by SakshiSharma
...