Self Type in Python Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report Self-type is a brand-new function added to Python 3.11. A method or function that returns an instance of the class it belongs to is defined by the self type. In situations where we wish to enforce that a method returns an instance of the class and not some other type, self-type is beneficial. Returning an instance of the class in PythonIn this example, we are going to write a simple program to explain when we are going to return to the class itself and what will happen. Python3 # Define a class class Car: def set_brand(self, brand: str) -> Car: self.brand = brand return self # call set_brand method Car().set_brand("Maruti") Output: This program will return an error that "name 'car' is not defined" Returning an instance of a class using Self type in PythonIn this example, we are going to resolve the error that we faced in the previous example by making the use of new Self Type in Python. By using Self we can able to make the function that returns an instance of a class. As we can see in the output when we are calling the function of a class it does not give any output and we are able to print objects using the print statement. Python3 # Import Self from typing import Self # Define a base class class Car: def set_brand(self, brand: str) -> Self: self.brand = brand return self # Define a child class class Brand(Car): def set_speed(self, speed: float) -> Self: self.speed = speed return self # Calling object inside print statement print(Car().set_brand("Maruti")) print(Brand().set_brand("Mar\ uti").set_speed(110.5)) print(type(Car().set_brand("Maruti"))) print(type(Brand().set_brand("Maruti").set_speed(110.5))) Output: <__main__.Car object at 0x000002365E33F3D0> <__main__.Brand object at 0x000002365E33F3D0> <class '__main__.Car'> <class '__main__.Brand'> Comment More infoAdvertise with us Next Article Types of Arguments in Python S sagar99 Follow Improve Article Tags : Python Python-Data Type Practice Tags : python Similar Reads self in Python class In Python, self is a fundamental concept when working with object-oriented programming (OOP). It represents the instance of the class being used. Whenever we create an object from a class, self refers to the current object instance. It is essential for accessing attributes and methods within the cla 6 min read self in Python class In Python, self is a fundamental concept when working with object-oriented programming (OOP). It represents the instance of the class being used. Whenever we create an object from a class, self refers to the current object instance. It is essential for accessing attributes and methods within the cla 6 min read Types of Arguments in Python Arguments are the values passed inside the parenthesis of the function. A function can have any number of arguments separated by a comma. There are many types of arguments in Python .In this example, we will create a simple function in Python to check whether the number passed as an argument to the 3 min read Python While Loop Python While Loop is used to execute a block of statements repeatedly until a given condition is satisfied. When the condition becomes false, the line immediately after the loop in the program is executed.In this example, the condition for while will be True as long as the counter variable (count) i 5 min read Python While Loop Python While Loop is used to execute a block of statements repeatedly until a given condition is satisfied. When the condition becomes false, the line immediately after the loop in the program is executed.In this example, the condition for while will be True as long as the counter variable (count) i 5 min read __init__ in Python Prerequisites - Python Class and Objects, Self__init__ method in Python is used to initialize objects of a class. It is also called a constructor. It is like a default constructor in C++ and Java. Constructors are used to initialize the objectâs state.The task of constructors is to initialize (assig 5 min read Like