Python __init__() - Working and Examples


Python __init__()

__init__() is a built-in function in Python that is called whenever an object is created. It initializes the state for the object, meaning it's where we can set the attributes of an object.

We can also pass arguments to the __init__() function so that each object, when created, can have unique attributes.

If __init__() is not defined in a class, there will be an implicit call to the built-in __init__() function.


Simple Example for __init__() in Python

In the following example, we have defined a class with an __init__() function, where we initialize some of the object parameters.

Python Program

class Laptop:
	
	def __init__(self, name, processor, hdd, ram, cost):
		self.name = name
		self.processor = processor
		self.hdd = hdd
		self.ram = ram
		self.cost = cost
		
	def details(self):
		print('The details of the laptop are:')
		print('Name         :', self.name)
		print('Processor    :', self.processor)
		print('HDD Capacity :', self.hdd)
		print('RAM          :', self.ram)
		print('Cost($)      :', self.cost)
		
#create object
laptop1 = Laptop('Dell Alienware', 'Intel Core i7', 512, 8, 2500.00)

print(laptop1.name)
print(laptop1.processor)

laptop1.details()

Explanation:

  1. The __init__() function is called when the Laptop object laptop1 is created. It initializes the object's attributes with the values passed as arguments.
  2. The attributes name, processor, hdd, ram, and cost are assigned to the object using the self reference.
  3. In the details() method, we access and print the initialized attributes of the object laptop1.
  4. The print() function prints the specific attributes directly, while the details() method prints all of them in a formatted manner.

Output

Dell Alienware
Intel Core i7
The details of the laptop are:
Name         : Dell Alienware
Processor    : Intel Core i7
HDD Capacity : 512
RAM          : 8
Cost($)      : 2500.0

Another Example: Default Parameters in __init__()

In this example, we use default parameters in the __init__() function so that if some arguments are not passed while creating the object, default values will be used.

Python Program

class Laptop:
	def __init__(self, name='Unknown', processor='Intel', hdd=500, ram=4, cost=1000):
		self.name = name
		self.processor = processor
		self.hdd = hdd
		self.ram = ram
		self.cost = cost
	
	def details(self):
		print('The details of the laptop are:')
		print('Name         :', self.name)
		print('Processor    :', self.processor)
		print('HDD Capacity :', self.hdd)
		print('RAM          :', self.ram)
		print('Cost($)      :', self.cost)

#create object without passing arguments
laptop1 = Laptop()

laptop1.details()

Explanation:

  1. Here, the __init__() function defines default values for all parameters.
  2. If no arguments are passed when creating the object laptop1, the default values are used. In this case, name='Unknown', processor='Intel', etc., are set by default.
  3. The details() method is then called to print out the object details with the default values.

Output

The details of the laptop are:
Name         : Unknown
Processor    : Intel
HDD Capacity : 500
RAM          : 4
Cost($)      : 1000

Example with Multiple Objects

In this example, we will create multiple objects using the same __init__() method but with different values for each.

Python Program

class Laptop:
	def __init__(self, name, processor, hdd, ram, cost):
		self.name = name
		self.processor = processor
		self.hdd = hdd
		self.ram = ram
		self.cost = cost

	def details(self):
		print(f'{self.name} Details:')
		print('Processor    :', self.processor)
		print('HDD Capacity :', self.hdd)
		print('RAM          :', self.ram)
		print('Cost($)      :', self.cost)

# Create multiple objects
laptop1 = Laptop('Dell Alienware', 'Intel Core i7', 512, 8, 2500.00)
laptop2 = Laptop('HP Spectre', 'Intel Core i5', 256, 8, 1200.00)

# Call details for both objects
laptop1.details()
laptop2.details()

Explanation:

  1. We create two objects, laptop1 and laptop2, with different values for their attributes.
  2. Each object is initialized using the __init__() function with different arguments.
  3. The details() method is called for both objects to display their specific details.

Output

Dell Alienware Details:
Processor    : Intel Core i7
HDD Capacity : 512
RAM          : 8
Cost($)      : 2500.0
HP Spectre Details:
Processor    : Intel Core i5
HDD Capacity : 256
RAM          : 8
Cost($)      : 1200.0

Summary

In this tutorial, we learned about the __init__() function in Python, how to use it in a class definition, and how to override the built-in __init__() function. We also covered default parameters, the creation of multiple objects, and the use of the self reference to set and access object attributes.


Python Libraries