Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

Understanding the __init__() method in Python

  • Deeptendu SantraDeeptendu Santra
  • March 31, 2021
  • Python OOPS
Understanding the __init__() method in Python

In this article, we discuss a concept of OOPs – The Python Constructor and how explain in detail how we can use __init__() method for initializing an object.

What is a constructor?

Before we go into the idea of constructor, here is a quick heads up on classes and objects in general:

“In OOP, object refers to an abstract data type created by a developer. An object is defined/characterized by it’s parameters(state-values) and behaviour(methods). Classes are a blueprint or a set of instructions to build a specific type of object.”

Constructors can very simply be understood as a special method that is called during the object initialization/creation. The constructor is usually defined as a function in the class definition that takes in the state parameters and creates an object with those user-defined parameters.

In python the constructor method is __init__(), and is written as:

def __init__(self, object_parameters...):
    # Initialize the object

The function takes in self and the object parameters as input. The object parameters as the name suggests are the state variables that define the object.

The ‘self’ is a reserved keyword in python which represents the instance of the class. The ‘self’ keyword allows easy access to the class methods and parameters for use in other methods of the class.

For example, An object state variable ‘var’ can be accessed using self.var.

This idea will become more clear when we see the example of creating a class.

Creating a simple class: Cartesian Point

In this example we create a class for the class for the 2D Cartesian points. The class has two state variables – x and y which define the point’s location.

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def coords(self):
        print("The point is at: ({}, {})".format(self.x, self.y))

The __init__() method takes self along with the two state variables as input. Then we initialise the object by setting the state variables of the object to the user-defined value.

The state variables of the object( here x and y) can be accessed using self.x and self.y respectively.

The other method here is the coords() that prints the current location of the point. Notice how we access the points using the self.x and self.y.

# Create object `P` of class Point with value 3 and 4
P = Point1(3, 4)

# Print the coordinates of point `P`
P.coords()
Image 23

Default Variables in the __init__() method

Like any other function the in python the __init__() method allows you to take default values. This feature can be particularly useful when a class takes in a lot of input parameters. Here we construct another class Point1 which initializes the point to (0, 0) is no data point is specified.

class Point1:
    def __init__(self, x=0, y=0):
        self.x = x
        self.y = y
    def coords(self):
        print("The point is at: ({}, {})".format(self.x, self.y))

# Create object of class Point1 with default parameters
Q = Point1()
Q.coords()
Image 22

Conclusion

We have seen how to define the constructors for a python class. Though is the most viable and easy method, turns out this is not the only method. Python allows more complicated class initialization, which especially becomes essential during inheriting/extending other classes. We will see more of such examples in future articles.

Deeptendu Santra
Deeptendu Santra
Articles: 19
Python Calendar module – 6 IMP functions to know!
Previous Post Python Calendar module - 6 IMP functions to know!
Next Post Python isna() and notna() functions from Pandas
Python isna() and notna() functions from Pandas

Related Posts

Polymorphism in Python: Examples and Use

Polymorphism in Python: Examples and Use

  • November 19, 2026
Object-Oriented Programming in Python: Use Classes

Object-Oriented Programming in Python: Use Classes

  • November 18, 2026
Inheritance in Python

Inheritance in Python

  • November 18, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.