0% found this document useful (0 votes)
19 views

Pythonquestion 21

__init__ is a constructor method in Python classes that is automatically called when a new object is created to allocate memory. It distinguishes class methods and attributes from local variables. The __init__ method takes in parameters like fname, lname, age, section and assigns them to attributes of the class like self.firstname, self.lastname, etc. to initialize a new Student object.

Uploaded by

ameetamarwadi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Pythonquestion 21

__init__ is a constructor method in Python classes that is automatically called when a new object is created to allocate memory. It distinguishes class methods and attributes from local variables. The __init__ method takes in parameters like fname, lname, age, section and assigns them to attributes of the class like self.firstname, self.lastname, etc. to initialize a new Student object.

Uploaded by

ameetamarwadi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 1

What is __init__?

__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 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")

You might also like