In this code:

class A(object):
    def __init__(self):
        self.x = 'Hello'

    def method_a(self, foo):
        print self.x + ' ' + foo

... the self variable represents the instance of the object itself. Most object-oriented languages pass this as a hidden parameter to the methods defined on an object; Python does not. You have to declare it explicitly. When you create an instance of the A class and call its methods, it will be passed automatically, as in ...

a = A()               # We do not pass any argument to the __init__ method
a.method_a('Sailor!') # We only pass a single argument

The __init__ method is roughly what represents a constructor in Python. When you call A() Python creates an object for you, and passes it as the first parameter to the __init__ method. Any additional parameters (e.g., A(24, 'Hello')) will also get passed as arguments--in this case causing an exception to be raised, since the constructor isn't expecting them.

Answer from Chris B. on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › __init__-in-python
__init__ in Python - GeeksforGeeks
__init__ method in Python is a constructor. It runs automatically when a new object of a class is created. Its main purpose is to initialize the object’s attributes and set up its initial state.
Published   September 12, 2025
Top answer
1 of 16
717

In this code:

class A(object):
    def __init__(self):
        self.x = 'Hello'

    def method_a(self, foo):
        print self.x + ' ' + foo

... the self variable represents the instance of the object itself. Most object-oriented languages pass this as a hidden parameter to the methods defined on an object; Python does not. You have to declare it explicitly. When you create an instance of the A class and call its methods, it will be passed automatically, as in ...

a = A()               # We do not pass any argument to the __init__ method
a.method_a('Sailor!') # We only pass a single argument

The __init__ method is roughly what represents a constructor in Python. When you call A() Python creates an object for you, and passes it as the first parameter to the __init__ method. Any additional parameters (e.g., A(24, 'Hello')) will also get passed as arguments--in this case causing an exception to be raised, since the constructor isn't expecting them.

2 of 16
318

Yep, you are right, these are oop constructs.

__init__ is the constructor for a class. The self parameter refers to the instance of the object (like this in C++).

class Point:
    def __init__(self, x, y):
        self._x = x
        self._y = y

The __init__ method gets called after memory for the object is allocated:

x = Point(1,2)

It is important to use the self parameter inside an object's method if you want to persist the value with the object. If, for instance, you implement the __init__ method like this:

class Point:
    def __init__(self, x, y):
        _x = x
        _y = y

Your x and y parameters would be stored in variables on the stack and would be discarded when the init method goes out of scope. Setting those variables as self._x and self._y sets those variables as members of the Point object (accessible for the lifetime of the object).

N.B. Some clarification of the use of the word "constructor" in this answer. Technically the responsibilities of a "constructor" are split over two methods in Python. Those methods are __new__ (responsible for allocating memory) and __init__ (as discussed here, responsible for initialising the newly created instance).

Discussions

Understanding classes, init and self
Could you explain this code excerpt? I am learning and trying to get in touch with the logic of classes · The __init__ method initialises a new instance (object) of the class NewWindow. The actual creation of the instance is done by a separate method __new__, before __init__ is called. More on discuss.python.org
🌐 discuss.python.org
19
0
February 16, 2024
What does __init__ and self do in python?
What does __init__ and self do in Python? __init__ is used to initialise the created class instance, in practice this mostly means setting the attributes the methods expect it to have. self refers to the current instance, although the name itself isn't special in any way whatsoever - you could use whatever valid identifier you wanted, although I don't particularly recommend deviating from established practices here because we're used to self. As for why your code doesn't do what you think, you never called that method. Random_Player = Game('Male', 19) Random_Player.score() As a side note I recommend reading PEP-8, PEP-257, and PEP-484 as the official style guide. More on reddit.com
🌐 r/learnpython
40
116
January 19, 2024
Please explain __init__ like ive never heard of programming before. Its just not making sense.
Remember that classes become their own entity when created. An instance of a class is an object. When you are defining what all is associated with that class object in your class definition, the object needs to know what on earth it has to do to "initialize" itself. That's what the init is doing. It's giving the information needed to say "this is what you do when you are created." I know you mentioned like you've never heard of programming but it's essentially the constructor. The "self" in the different spots tells the new object what it owns. Having "self.name" means that class object has a "name" associated with it and the name belongs to that class object. The arguments you are passing in do not belong to that class object. Think of it like a fenced in area if a field. The class object is the fenced in area. That init is acting like a gate allowing in pieces of information (name major gpa) to be seen by the internal area. The init function is telling itself, hey these pieces of information from the outside are important and I need to remember them, so I'm going to copy them and store them in different sections of my internally fenced field. That way if I need to, I can go to that corner of the field and see what my "name" is. Every class you create will need to know what to do when it is created, so the init is needed. If you created a generic function like you described, that makes it a function, and not a class, and thus not something that can be created as its own object. Referring back to the fenced in area, by creating a class, you've created a fenced in area that is its own section and can store and remember to do things as it pleases with access to anything inside that fenced in area. Making it a function, removes the fence. If I'm running through the field and there is no fence, I'm just going to keep on running past everything and that function use gets left behind. If a class was made, I can revisit that fenced in area for specific pieces of information if I so choose, because I see where the fence is and can run back to it. More on reddit.com
🌐 r/learnpython
67
480
February 17, 2021
python - What is __init__.py for? - Stack Overflow
The __init__.py file can contain ... and Python will add some additional attributes to the module when it is imported. But just click the link, it contains an example, more information, and an explanation of namespace packages, the kind of packages without __init__.py. ... Sign up to request clarification or add additional context in comments. ... What does this mean: "this is ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
StrataScratch
stratascratch.com › blog › what-is-the-purpose-of-__init__-in-python
What Is the Purpose of __init__ in Python? - StrataScratch
November 7, 2024 - The __init__ method in Python initializes an object's attributes at creation, setting up each instance with specific values to keep code organized and scalable.
🌐
Great Learning
mygreatlearning.com › blog › it/software development › python __init__: an overview
Python __init__: An Overview
October 14, 2024 - python __init__: In Python, __init__ is a special method known as the constructor.
🌐
Mimo
mimo.org › glossary › python › init-function
Python __init__() Function: Syntax, Usage, and Examples
The Python __init__() function serves as the constructor method for classes. It initializes newly created objects and allows you to assign values to object properties or run startup procedures when an instance is created.
🌐
W3Schools
w3schools.com › python › gloss_python_class_init.asp
Python __init__() Function
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... The examples above are classes and objects in their simplest form, and are not really useful in real life applications. To understand the meaning of classes we have to understand the built-in __init__() method.
🌐
Real Python
realpython.com › python-init-py
What Is Python's __init__.py For? – Real Python
July 9, 2025 - The __init__.py file is a Python source file, which means that it’s also a module.
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › what does __init__ and self do in python?
r/learnpython on Reddit: What does __init__ and self do in python?
January 19, 2024 -

I don't really understand, I read many reddit posts, websites about it but still I can't use it properly.I don't know what does it do, what's the point of having it

I tried a code like this but the result is not what I expected. I thought it prints "wow" or "goodbye".

class Game():
    def __init__(self, Character, Age):
        self.Character = Character
        self.Age = Age

    def score(self):
        if self.Age >= 18:
           print("wow")
        else:
           print("goodbye")

Random_Player = Game('Male', 19) 
print(Random_Player)

Results

<__main__.Game object at 0x0000013AD63D85D0>
Top answer
1 of 21
93
What does __init__ and self do in Python? __init__ is used to initialise the created class instance, in practice this mostly means setting the attributes the methods expect it to have. self refers to the current instance, although the name itself isn't special in any way whatsoever - you could use whatever valid identifier you wanted, although I don't particularly recommend deviating from established practices here because we're used to self. As for why your code doesn't do what you think, you never called that method. Random_Player = Game('Male', 19) Random_Player.score() As a side note I recommend reading PEP-8, PEP-257, and PEP-484 as the official style guide.
2 of 21
34
Game is a class with 2 member variables and one method attached to it. __init__ is called when you create a new instance of the Game class. It expects two arguments, and assigns them to the member variables of the object. The function score checks the Age member and prints one of two messages depending on what value Age holds. So when you run the line Random_Player = Game('male' 19) You create a new instance of a Game object. __init__ is called, which makes the assignments to the member variables, and the new object is then assigned to the variable Random_Player. On the next line you print Random_Player. Printing anything that's not already a string causes Python to try to convert it to a string. It does this by looking for two functions in the class: __str__ or __repr__. Without going into more detail, if either of these exist, Python will use that function to convert any Game object to a string when asked. You'll notice that like __init__ both of those functions use double underscores. These are special functions that have specific meanings when you define them for a class, and option will use them automatically for different things. Since neither of those exist however, it uses the default functionality built into Python, printing out the name of the type and the memory address that specific variable is being stored at. Nowhere in the code do you ever call the score function, so that code is never run. If you want it to run, you call it like: Random_Player.score() Note that you don't print the call to score, the score function calls print itself, so all you need to do is call that function and it will print out the message based on Random_Player.Age. Hopefully this helps you understand how all these things piece together, and why your code isn't working in write the way you expect.
🌐
Reddit
reddit.com › r/learnpython › please explain __init__ like ive never heard of programming before. its just not making sense.
r/learnpython on Reddit: Please explain __init__ like ive never heard of programming before. Its just not making sense.
February 17, 2021 -

'''

class Student:

def __init__(self, name, major, gpa)

    self.name = name

    self.major = major

    self.gpa = gpa

student1 = Student("Bob", "Art", 3.0)

'''

Why do i need to use the init and self function? Do i have to make a new init for every single class?

Why cant i just make a regular function like

def Student(name, major, gpa) ??

Top answer
1 of 5
273
Remember that classes become their own entity when created. An instance of a class is an object. When you are defining what all is associated with that class object in your class definition, the object needs to know what on earth it has to do to "initialize" itself. That's what the init is doing. It's giving the information needed to say "this is what you do when you are created." I know you mentioned like you've never heard of programming but it's essentially the constructor. The "self" in the different spots tells the new object what it owns. Having "self.name" means that class object has a "name" associated with it and the name belongs to that class object. The arguments you are passing in do not belong to that class object. Think of it like a fenced in area if a field. The class object is the fenced in area. That init is acting like a gate allowing in pieces of information (name major gpa) to be seen by the internal area. The init function is telling itself, hey these pieces of information from the outside are important and I need to remember them, so I'm going to copy them and store them in different sections of my internally fenced field. That way if I need to, I can go to that corner of the field and see what my "name" is. Every class you create will need to know what to do when it is created, so the init is needed. If you created a generic function like you described, that makes it a function, and not a class, and thus not something that can be created as its own object. Referring back to the fenced in area, by creating a class, you've created a fenced in area that is its own section and can store and remember to do things as it pleases with access to anything inside that fenced in area. Making it a function, removes the fence. If I'm running through the field and there is no fence, I'm just going to keep on running past everything and that function use gets left behind. If a class was made, I can revisit that fenced in area for specific pieces of information if I so choose, because I see where the fence is and can run back to it.
2 of 5
46
Corey Schafer has a series about OOP in python which cleared a lot of doubts for me personally. Give it a shot. https://youtube.com/playlist?list=PL-osiE80TeTsqhIuOqKhwlXsIBIdSeYtc
🌐
Index.dev
index.dev › blog › what-is-init-in-python
Master Python Objects: The Ultimate Guide to the __init__ Method
This unique function is triggered automatically when a class creates an object. This function lets us do any setup or configuration chores required and initialise the properties of an object.
Top answer
1 of 14
2111

It used to be a required part of a package (old, pre-3.3 "regular package", not newer 3.3+ "namespace package").

Here's the documentation.

Python defines two types of packages, regular packages and namespace packages. Regular packages are traditional packages as they existed in Python 3.2 and earlier. A regular package is typically implemented as a directory containing an __init__.py file. When a regular package is imported, this __init__.py file is implicitly executed, and the objects it defines are bound to names in the package’s namespace. The __init__.py file can contain the same Python code that any other module can contain, and Python will add some additional attributes to the module when it is imported.

But just click the link, it contains an example, more information, and an explanation of namespace packages, the kind of packages without __init__.py.

2 of 14
1356

Files named __init__.py are used to mark directories on disk as Python package directories. If you have the files

mydir/spam/__init__.py
mydir/spam/module.py

and mydir is on your path, you can import the code in module.py as

import spam.module

or

from spam import module

If you remove the __init__.py file, Python will no longer look for submodules inside that directory, so attempts to import the module will fail.

The __init__.py file is usually empty, but can be used to export selected portions of the package under more convenient name, hold convenience functions, etc. Given the example above, the contents of the init module can be accessed as

import spam

This answer is based on this webpage.

🌐
Finxter
blog.finxter.com › python-init
What is __init__ in Python? – Be on the Right Side of Change
Click the image to get the cheat sheet (opens in a new tab). If you’re already comfortable with basic object-orientation terminologies like classes and instances, simply read on. You’ve learned that the __init__ method is the constructor method of a class.
🌐
Analytics Vidhya
analyticsvidhya.com › home › all about init in python
All About init in Python - Analytics Vidhya
February 1, 2024 - Visualizing Patterns and Trends ... crucial in object-oriented programming in Python. It is a special method automatically called when an object is created from a class....
🌐
Python Morsels
pythonmorsels.com › what-is-init
What is __init__ in Python? - Python Morsels
February 11, 2021 - The __init__ method is used to initialize a class. The initializer method accepts self (the class instance) along with any arguments the class accepts and then performs initialization steps.
🌐
Built In
builtin.com › data-science › new-python
__new__ vs. __init__ Methods in Python | Built In
The __init__ method is an instance method that automatically initializes a newly created object. While it operates on an instance, it’s not called directly by the programmer, and is invoked automatically by Python after the __new__ method ...
🌐
Udacity
udacity.com › blog › 2021 › 11 › __init__-in-python-an-overview.html
__init__ in Python: An Overview | Udacity
May 10, 2022 - Python uses the str class that ... all happening in the background. The __init__ method is the Python equivalent of the C++ constructor in an object-oriented approach....
🌐
MicroPyramid
micropyramid.com › blog › understand-self-and-__init__-method-in-python-class
Understanding Self and __init__ Method in Python Class | MicroPyramid
self represents the instance of the class. By using the "self" keyword we can access the attributes and methods of the class in python. "__init__" is a reseved method in python classes. It is known as a constructor in object oriented concepts.
🌐
Medium
medium.com › @piyushkashyap045 › understanding-the-use-case-of-init-in-python-3de27bf9b529
Understanding the Use Case of __init__ in Python | by Piyush Kashyap | Medium
June 23, 2024 - The __init__ method is an integral part of Python's object-oriented programming, providing a robust mechanism for initializing objects. By setting initial values, validating input, defining default values, and allocating resources, __init__ ...
🌐
Scaler
scaler.com › home › topics › __init__ in python
__init__ in Python - Scaler Topics
December 1, 2023 - __init__() is a special Python method that runs when an object of a class is created. __init__() function is mostly used for assigning values to newly created objects. __init__() is a magic method, which means it is called automatically by Python