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 of the class itself — the specific object you are working with — and Python passes it automatically as the first argument 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 constructor.
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
Python class __init__ and self
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 that mean? More on discuss.python.org
🌐 discuss.python.org
7
0
October 4, 2024
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
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
🌐
GeeksforGeeks
geeksforgeeks.org › python › __init__-in-python
__init__ in Python - GeeksforGeeks
class Dog: def __init__(self, name, breed="Mixed", age=1): self.name = name self.breed = breed self.age = age a = Dog("Buddy") b = Dog("Max", "Golden Retriever", 5) print(a.name, a.breed, a.age) print(b.name, b.breed, b.age) ...
Published: May 11, 2026
🌐
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.

🌐
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 - Its main job is to initialize the object’s attributes — that is, to set them up with the data they need. class MyClass: def __init__(self, arg1, arg2): self.arg1 = arg1 self.arg2 = arg2
🌐
Great Learning
mygreatlearning.com › blog › it/software development › python __init__: an overview
Python __init__: An Overview
October 14, 2024 - Here's an example to illustrate the usage of __init__: class MyClass: def __init__(self, name, age): self.name = name self.age = age def display_info(self): print(f"Name: {self.name}") print(f"Age: {self.age}") # Creating an instance of MyClass ...
Find elsewhere
🌐
Edureka
edureka.co › blog › init-in-python
Init In Python | Python Init Class | What is Init Function | Edureka
November 27, 2024 - Each of the cars that you create within the game will all have different attributes, for example color, speed etc. as well as methods like change gear, accelerate, break etc. When you code this concept into the Python interpreter it should look something like this. class Car(object): """ blueprint for car """ def __init__(self, model, color, company, speed_limit): self.color = color self.company = company self.speed_limit = speed_limit self.model = model def start(self): print("started") def stop(self): print("stopped") def accelarate(self): print("accelarating...") "accelarator functionality here" def change_gear(self, gear_type): print("gear changed") " gear related functionality here" Now that we have created the objects, let’s move on to create the individual cars in the game.
🌐
Medium
medium.com › @ppraveen4a8 › self-and-init-method-in-python-3e55af7d362b
Self and InIt Method in Python. In this blog will discuss about why do… | by Praveen Kumar | Medium
November 25, 2023 - so self refers to obj1 variables and methods. we can access the method variable with “obj1.name”. In this way we can access the attributes inside the class · Now we will see how we can access the method inside the class. Understand this using the below code. class A: def __init__(self,name,gender,mobile_number): self.name=name self.gender=gender self.mobile_number=mobile_number def printing_name(self): print("The Name Of the Object is ", self.name) def printing_gender(self): print("Gender is ",self.gender) def printing_mobile_number(self): print("Mobile Number is ", self.mobile_number) ob
🌐
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…
🌐
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")
🌐
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) ...
🌐
W3Schools
w3schools.com › PYTHON › python_class_init.asp
Python __init__() Method
Create a class named Person, use the __init__() method to assign values for name and age: class Person: def __init__(self, name, age): self.name = name self.age = age p1 = Person("Emil", 36) print(p1.name) print(p1.age) Try it Yourself »
🌐
Index.dev
index.dev › blog › what-is-init-in-python
Master Python Objects: The Ultimate Guide to the __init__ Method
It's called "Person." This class has a unique method named "init" that is called when an object of the class is made. In this way, the method gets two options, "name" and "age," which can be used to change the Person object's "name" and "age" properties. In the end, we make a Person object with the name "Maria" and the age of 25....
🌐
Python Tutorial
pythontutorial.net › home › python oop › python __init__
Python __init__
March 31, 2025 - Since Python will automatically ... defines the Person class with the __init__() method: class Person: def __init__(self, name, age): self.name = name self.age = age person = Person('John', 25) print(f"I'm {person.name}. I'm {person.age} years old.") Code language: Python ...
🌐
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 - You can also store your books into objects in Python, and each object would have some attributes that are important to know. First, you make the blueprint of the object (the class) and then you start creating new books: ... class Book: def __init__(self, title, author, genre): self.title = ...
🌐
Mimo
mimo.org › glossary › python › init-function
Python __init__() Function: Syntax, Usage, and Examples
This example highlights a basic init Python structure used to initialize an object with attributes. Under the hood, the first argument to __init__() is always the instance itself, commonly named self.
🌐
Besant Technologies
besanttechnologies.com › home › blogs › general › init in python
Init in Python | Python Init Class | Python _Init_() Function
October 8, 2021 - Get Placement Oriented Python Training from Industry Experts with our Python Training in Chennai · >>> class cylinder def init_(self, height, radius) self.height = height self.radius = radius def get volume of_cylinder(self) return (self.height ...
🌐
Udacity
udacity.com › blog › 2021 › 11 › __init__-in-python-an-overview.html
__init__ in Python: An Overview | Udacity
June 10, 2026 - When you write UserAccount("sam", "sam@example.com"), Python creates the object, then calls __init__ to store "sam" and "sam@example.com" as attributes on that specific instance. The confusion around __init__ in Python is real.