Initialization class properties in Python
The class provide function __init__() to initialize the class object by providing the intial value as parameters. An init function is always executed when the class is being initiated. It assign values to object properties or it allows to perform operation which required before creating instance of the class.
Syntax
 1 class Employee:
 2   def __init__(self, <parameters>):
 3     <initialization>
parameters : A parameters which used to initialize object properties
initialization : An initialization of class properties
Class __init__() function
 1 # define class
 2 class Employee:
 3   # initialize object instance properties
 4   def __init__(self, id, name, salary):
 5     self.id = id
 6     self.name = name
 7     self.salary = salary
 8 
 9 # create class object with initial value
 10 emp = Employee(10, "James", "10000")
 11 
 12 print("Employee ID :", emp.id)
 13 print("Employee name :", emp.name)
 14 print("Employee salary :", emp.salary)
In the above example, a class Employee is created with initialization method that accept four parameters including self parameters. A class properties are initialize with parameters value. The class instance is created and the properties value will be printed on console.
Output
 1 Employee ID : 10
 2 Employee name : James
 3 Employee salary : 10000
Note : An __init__() function will be called automatically whenever class instance is being used to create a new object using constructor.
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us