+ 2
What is the reason
Why init method is use what is the advantage
2 RĂ©ponses
+ 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.
+ 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)