Python Class __init__ Method Tutorial

Python is an object-oriented programming language where the class is used to create new instances or objects. As class contains different data types and structures during the creation of the instance these data should be initialized properly. __init__ method is used to initialize a Python object during the creation. The __init__ method can be called as a constructor which exists in popular object-oriented programming languages like C++, Java, C#, etc.

__init__ Method Syntax

The __init__ method is a function there defined inside the class definition. This method can be accepts sinlge or multiple parameters according to design.

class Human:
   ...
   def __init__(self,PARAMETERS):
      INIT_BLOCK
   ...
  • def __init__ is used to define the __init__ as function.
  • self is the class instance in order to access the instance or object inside the __init__ function and change and set values.
  • PARAMETERS are non, single, or more parameters which is used to provide values during the object or instance creation. PARAMETERS generally used to set values of the object or instance.

__init__ Method with No Parameter

__init__ method can be used with no parameter. In this case, the values of the instance will be set to the default values or nothing will be done. Even this may seem a bit strange because we can set object values during the definition of the variables using __init__ is a more elegant and structured way.

class Human:
   name = ""
   def __init__(self):
      self.name="NoName"

human = Human()

print(human.name)
__init__ Method with No Parameter

__init__ Method with Single Parameter

We can also use the __init__ method in order to accept single parameter. We can provide a value to the __init__ method.

class Human:
   name = ""
   def __init__(self , name ):
      self.name= name

human1 = Human("ahmet")

print(human1.name)


human2 = Human("ali")

print(human2.name)


human3 = Human("elif")

print(human3.name)
__init__ Method with Single Parameter

__init__ Method with Multiple Parameters

The most popular usage of the __init__ method is using it will multiple parameters where multiple values can be set or change. We will provide these multiple parameter like a function parameter.

class Human:
   name = ""
   surname = ""
   id = 0
   def __init__(self , name , surname , id ):
      self.name= name
      self.surname = surname
      self.id = id

human1 = Human( "ahmet" , "baydan" , 1 )

print( human1.name )


human2 = Human( "ali" , "baydan" , 2 )

print( human2.surname )


human3 = Human( "ahmet" , "baydan" , 3 )

print( human3.id )
__init__ Method with Multiple Parameters

__init__ Method with Default Parameter

Even __init__ method parameters can be provided during the creation of the object also some default values can be be used without providing them explicitly. In the following example, we will set the gender parameter as default to “man”. This means if the gender parameter is not provided by default the “man” value will be used. If provided the provided value will be used.

class Human:
   name = ""
   surname = ""
   id = 0
   gender= ""
   def __init__(self , name , surname , id , gender="man" ):
      self.name= name
      self.surname = surname
      self.id = id
      self.gender = gender

human1 = Human( "ahmet" , "baydan" , 1 )

print( human1.name )


human2 = Human( "ali" , "baydan" , 2 , "man")

print( human2.surname )


human3 = Human( "ahmet" , "baydan" , 3 , "woman" )

print( human3.id )
__init__ Method with Default Parameter

Leave a Comment