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
Top answer
1 of 16
719

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).

🌐
MicroPyramid
micropyramid.com › home › blog › python › understanding self and __init__ in python classes
self and __init__ in Python: A Complete Guide | MicroPyramid
June 9, 2026 - In Python, self is the instance ... to every instance method. __init__ is the method Python runs right after a new object is created so you can set up its starting attributes; it is an initializer, not a construc...
Discussions

Can Someone explain to me what is self and init in python?
Sorry to comment on such an old post, but I thought I'd respond anyway just in case anyone else stumbles across this. I had a very hard time understanding self at first too. This video is what finally made it 'click' for me, and hundreds of people in the comments said the same, so I highly suggest you give it a watch. You can skip to the part where he's using self if you'd like, but I think watching the whole video helps provide more context. That whole series is an excellent watch in general if you want to learn OOP. Why use 'self'? Imagine you had the following empty class: class Employee: pass You could assign a first_name and last_name variable to a new instance of this class like so: emp_1 = Employee() emp_1.first_name = "John" emp_1.last_name = "Doe" This is perfectly valid code, but imagine if you wanted to create many more employees with more variables such as email, phone_number, etc., for each. It would take up a lot of lines and there would also be repeated code. A better approach would instead be to define an __init__ method like so: class Employee: def __init__(self, first, last): self.first_name = first self.last_name = last I'll explain the above in a moment, but just know that we can now create new employees like this: emp_2 = Employee("Johnny", "Doey") emp_3 = Employee("Mark", "Smith") emp_4 = Employee("Jack", "Walker") Instead of like this: emp_2 = Employee() emp_2.first_name = "Johnny" emp_2.last_name = "Doey" emp_3 = Employee() emp_3.first_name = "Mark" emp_3.last_name = "Smith" emp_4 = Employee() emp_4.first_name = "Jack" emp_4.last_name = "Walker" The first way is much nicer, right? Of course, this is only 1 of the many benefits of using self, but I think it's a good example to start with nevertheless. So what actually is init and 'self'? __init__ (simply meaning 'initialize') is a special method that is called immediately after an instance of a class is created. It's the equivalent of the constructor in other languages such as C#. The self parameter just refers to a specific instance of the object itself. It does not need to be called 'self', but doing so is a very popular convention. You could technically replace self with something like emp in this case, if you wanted. You might be wondering "Where is this self parameter even coming from? I don't see it being passed in as an argument anywhere?". Well, when you instantiate a new object in Python, self is automatically passed through as the first argument, so you do not need to include it yourself. However, since self is kind of 'secretly' being passed in, this means that it now needs to be accepted as the first parameter in the __init__ method. Again, self refers to whichever object you are currently instantiating. In the below example, it refers to emp_1 , since that's the name of our variable. emp_1 = Employee("John", "Doe") In the above example, when emp_1 is instantiated, the __init__ method assigns "John" to first_name and "Doe" to last_name for the emp_1 object (self in this case), since those are the arguments that were passed through during instantiation. def __init__(self, first, last): self.first_name = first self.last_name = last More on reddit.com
🌐 r/learnpython
45
261
November 29, 2022
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. class NewWindow(tk.Toplevel): def __init__(self): super().__init__() self.title("Another window") More on discuss.python.org
🌐 discuss.python.org
19
0
February 16, 2024
What do __init__ and self do in Python?
In Python's object-oriented programming (OOP), __init__ and self are fundamental concepts that facilitate the creation and management of objects. Grasping their roles is essential for writing effective and maintainable Python code. More on designgurus.io
🌐 designgurus.io
1
10
November 23, 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
118
January 19, 2024
People also ask

What does self mean in Python?
self is the instance the method is operating on. When you call obj.method(), Python passes obj in as the first argument, which you name self. You use it to read and set that object's attributes (self.color) and to call its other methods (self.get_area()).
🌐
micropyramid.com
micropyramid.com › home › blog › python › understanding self and __init__ in python classes
self and __init__ in Python: A Complete Guide | MicroPyramid
Is __init__ a constructor?
Not strictly. __init__ is an initializer: it runs on an object that already exists to set its starting attributes. The actual construction (allocating the object) is done by __new__, which Python calls first. In casual conversation people often call __init__ "the constructor", but the precise answer is that __new__ creates and __init__ initialises.
🌐
micropyramid.com
micropyramid.com › home › blog › python › understanding self and __init__ in python classes
self and __init__ in Python: A Complete Guide | MicroPyramid
Why are mutable default arguments dangerous in __init__?
Default argument values are evaluated once when the function is defined, so a default like items=[] is a single list shared by every instance — mutating it on one object affects all others. Use items=None and create a fresh list inside __init__, or use field(default_factory=list) in a dataclass.
🌐
micropyramid.com
micropyramid.com › home › blog › python › understanding self and __init__ in python classes
self and __init__ in Python: A Complete Guide | MicroPyramid
🌐
Python.org
discuss.python.org › python help
Python class __init__ and self - Python Help - Discussions on Python.org
October 4, 2024 - My question what do self an init mean in python class? like this: class Complex: def __init__(self, realpart, imagpart): self.r = realpart self.i = imagpart so why I have to do that? And what do th…
🌐
Reddit
reddit.com › r/learnpython › can someone explain to me what is self and init in python?
r/learnpython on Reddit: Can Someone explain to me what is self and init in python?
November 29, 2022 -

I tried learning OOP from many video tutorials and documentation but I'm not understanding why we really need it or what is the use of it.

So it would be really helpful if someone could explain to me like a child what is the need for self and init in python.

Also, If you could tell me how did you fully grasp the concept of OOP in Python that would be really beneficial to me.

Thank You.

🌐
Educative
educative.io › answers › what-is-def-init-self-in-python
What is def __init__ self in Python?
In Python, _init_ behaves like a constructor for a class. _init_ is called whenever a classes’ object is created. self represents a class’s instance and is required to access any variables or methods within the class.
🌐
Great Learning
mygreatlearning.com › blog › it/software development › python __init__: an overview
Python __init__: An Overview
October 14, 2024 - The python __init__ method is declared within a class and is used to initialize the attributes of an object as soon as the object is formed. While giving the definition for an __init__(self) method, a default parameter, named ‘self’ is always ...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › __init__-in-python
__init__ in Python - GeeksforGeeks
Python · class Person: def ... b.age) Output · Max 30 Emilia 25 · Explanation: self: refers to the current object (always the first parameter)....
Published: May 11, 2026
🌐
W3Schools
w3schools.com › python › python_class_init.asp
Python __init__() Method
class Person: def __init__(self, name, age, city, country): self.name = name self.age = age self.city = city self.country = country p1 = Person("Linus", 30, "Oslo", "Norway") print(p1.name) print(p1.age) print(p1.city) print(p1.country) Try it Yourself »
🌐
Medium
medium.com › @siddharthchobhe › understanding-init-and-self-in-python-with-simple-examples-72fd08b19dc3
🔍 Understanding __init__ and self in Python (with Simple Examples) | by Siddharth Chobhe | Medium
April 9, 2025 - In Python, __init__ is a constructor method. It’s automatically called whenever you create a new object from a class. Its main job is to initialize the object’s attributes — that is, to set them up with the data they need. class MyClass: ...
🌐
Index.dev
index.dev › blog › what-is-init-in-python
Master Python Objects: The Ultimate Guide to the __init__ Method
The __init__ function in Python is defined within a class and serves the purpose of initialising the characteristics of an object immediately upon its creation. When defining a __init__(self) method, a default parameter called 'self' is always ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › self-in-python-class
self in Python - GeeksforGeeks
July 4, 2026 - When you call a method using an object, Python automatically passes that object as the first argument to the method. When defining methods inside a class, the first parameter is always self.
🌐
Udacity
udacity.com › blog › 2021 › 11 › __init__-in-python-an-overview.html
__init__ in Python: An Overview | Udacity
June 10, 2026 - They were created from the same class. Each one holds different data. That is because self pointed to a different instance each time __init__ ran. ... It appears as the first parameter in the method definition. You do not type it when you call the class. Python passes the instance automatically.
🌐
Python.org
discuss.python.org › python help
Understanding classes, init and self - Python Help - Discussions on Python.org
February 16, 2024 - Could you explain this code excerpt? I am learning and trying to get in touch with the logic of classes. class NewWindow(tk.Toplevel): def __init__(self): super().__init__() self.title("Another window")
🌐
Medium
medium.com › @mamchinn › pythons-init-method-and-self-the-key-to-understanding-object-oriented-programming-43003eb0c6c2
Python’s __init__ Method and self: The Key to Understanding Object-Oriented Programming | by Andrew Mamchyn | Medium
March 3, 2023 - The only difference is that Python runs this function automatically and we don’t need to call it ourselves. With dunder init method out class will look like this: class Person: def __init__(self, name): self.name = name def greeting(self): print(f"Hello there.
🌐
C# Corner
c-sharpcorner.com › blogs › usage-of-init-and-self-in-python-classes
Usage of __init__ and Self in Python Classes
class Employee: def __init__(self, name, department,paid): self.name = name self.department = department self.paid = paid def get_department(self): return self.department def get_PaidInfo(self): return self.paid obj = Employee("Ajay", "Sales", "Paid") print("Name of the Employee: "+obj.name) print("Department: "+obj.get_department()) print("Payment Status: "+obj.get_PaidInfo())
Top answer
1 of 1
10
Understanding __init__ and self in Python In Python's object-oriented programming (OOP), __init__ and self are fundamental concepts that facilitate the creation and management of objects. Grasping their roles is essential for writing effective and maintainable Python code. Let's explore what each of these does, why they're important, and how to use them properly. What is __init__? Definition __init__ is a special method in Python known as the initializer or constructor. It is automatically called when a new instance of a class is created. The primary purpose of __init__ is to initialize the object's attributes with values provided during instantiation. Syntax class ClassName: def __init__(self, parameters): # Initialization code self.attribute = value Example class Person: def __init__(self, name, age): self.name = name # Initializing the 'name' attribute self.age = age # Initializing the 'age' attribute # Creating an instance of Person person1 = Person("Alice", 30) print(person1.name) # Output: Alice print(person1.age) # Output: 30 Explanation: Class Definition: The Person class has an __init__ method that takes name and age as parameters. Attribute Initialization: Inside __init__, self.name and self.age are set to the values passed during object creation. Object Creation: When person1 = Person("Alice", 30) is executed, Python calls the __init__ method to initialize person1's attributes. What is self? Definition self is a conventionally named parameter in Python that refers to the instance of the class. It allows access to the attributes and methods of the class in Python. While self is not a reserved keyword, it is strongly recommended to use self to maintain readability and consistency. Role of self Accessing Instance Attributes: self is used to access or modify the attributes of the class instance. Calling Other Methods: It allows methods within the class to call other methods or access attributes. Distinguishing Between Instance and Local Variables: Helps differentiate between instance attributes and local variables within methods. Example class Circle: def __init__(self, radius): self.radius = radius # Using self to set the instance attribute def area(self): return 3.14 * (self.radius ** 2) # Accessing the instance attribute using self def resize(self, new_radius): self.radius = new_radius # Modifying the instance attribute using self # Creating an instance of Circle circle1 = Circle(5) print(circle1.area()) # Output: 78.5 circle1.resize(10) print(circle1.area()) # Output: 314.0 Explanation: Initialization: The __init__ method initializes the radius attribute using self.radius = radius. Accessing Attributes: The area method calculates the area by accessing self.radius. Modifying Attributes: The resize method changes the radius attribute using self.radius = new_radius. Why Are __init__ and self Needed? Object Initialization: __init__ allows you to set up the initial state of an object by initializing its attributes. Instance-Specific Data: self ensures that each object maintains its own state, allowing multiple instances of a class to have different attribute values. Encapsulation: Together, they promote encapsulation by bundling data (attributes) and methods that operate on the data within the class.
🌐
Reddit
reddit.com › r/learnpython › what does __init__ and self do in python?
What does __init__ and self do in python? : r/learnpython
January 19, 2024 - The Book class example is just to show how Python classes work. The __init__ method is used for initializing an object's initial state, and self refers to the specific instance of the object, allowing each instance to have its own data.
🌐
Analytics Vidhya
analyticsvidhya.com › home › all about init in python
All About init in Python - Analytics Vidhya
February 1, 2024 - The name “__init__” is a convention ... method is straightforward. It is a special method that takes at least one argument, typically named “self,” which refers to the instance of the class....
🌐
W3Schools
w3schools.com › python › gloss_python_class_init.asp
Python __init__() Function
class Person: def __init__(self, name, age): self.name = name self.age = age p1 = Person("John", 36) print(p1.name) print(p1.age) Try it Yourself » · Note: The __init__() method is called automatically every time the class is being used to create a new object. Python Syntax Tutorial Class Create Class Object Methods self Modify Object Properties Delete Object Properties Delete Object Class pass Statement ·