What is the reason | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is the reason

Why init method is use what is the advantage

6th May 2020, 10:35 AM
ใ€ŠA$# โ˜† $ING#ใ€‹
ใ€ŠA$# โ˜† $ING#ใ€‹ - avatar
2 Answers
+ 3
__init__ is a special inbuilt function that is generally used to initialize an object with class attributes. You can consider it as a constructor which gets called every time an instance of a class is created. You need not use it if you don't want to because a user defined method can still do the same job.
6th May 2020, 10:41 AM
Avinesh
Avinesh - avatar
+ 3
init method is used to initialize the class instance variable. Please refer below code: class Planet: def __init__(self, name, radius_metres, mass_kilograms, orbital_period_seconds, surface_temperature_kelvin): self.name = name self.radius_metres = radius_metres self.mass_kilograms = mass_kilograms self.orbital_period_seconds = orbital_period_seconds self.surface_temperature_kelvin = surface_temperature_kelvin pluto = Planet(name='Pluto', radius_metres=1184e3, mass_kilograms=1.305e22, orbital_period_seconds=7816012992, surface_temperature_kelvin=123) print(pluto.name)
6th May 2020, 10:45 AM
$ยข๐Žโ‚น๐”ญ!๐จ๐“
$ยข๐Žโ‚น๐”ญ!๐จ๐“ - avatar