Register Login

SELF in Python

Every professional or student using Python will have to create an instance of a class anytime during his programming career. We use it using the self keyword. Programmers use it in method definition or initialization of variables. This article is about the self parameter along with what is self in Python? What does it do? How to use it and some other crucial topics related to SELF.

What is Self?

The self parameter is a reference to the instance of a class. It aids in accessing the variables which belong to a class. One can use any other name like mine or me instead of self. It should be the first parameter in the function of a class while defining the function.

Here are 2 example:

Program:

class chkr:
    def __init__(self):
        print("The address of self in this class is: ", id(self))
 
obj = chkr()
print("The address of class object is: ", id(obj))

Program:

class Student:
  def __init__(myobj, name, age):
    myobj.name = name
    myobj.age = age

  def myfunc(abc):
    print("Hello my name is " + abc.name)

p1 = Student("Joydeep", 25)
p1.myfunc()

OUTPUT:

Here is another example of using the self parameter:

Program:

class sucar():
	
	# init method or constructor
	def __init__(self, name, color):
		self.name = name
		self.color = color
		
	def show(self):
		print("The car's name is", self.name )
		print("And, its color is", self.color )
		
# both the objects have a different self that contains their attributes
Supra = sucar("Supra", "blue")
Mercedes = sucar("Mercedes", "green")

Supra.show()	 # same output as car.show(Supra)
Mercedes.show() # same output as car.show(Mercedes)

OUTPUT:

Is the self a keyword?

Self acts as a keyword in other programming languages like C and C++. In Python, self is a parameter in a method definition. Professionals state that using self enhances the code readability. One can replace self with anything else as the first parameter of a method like mine, obj, etc.

Follow the below example:

class myClass: 
def show(other):
print(“other in place of self”)

Why self ought to be the first parameter of instance methods

Let's take the help of an example:

Program:

class Rect ():
def __init__(self, x = 0, y = 0):
self.x = x
self.y = y
def ar (self):
return (self.x * self.y)
rec1=Rect(6,10)
print ("The rectangle's area is:", rec1.ar())

Output:

In the above example, __init__ passed two arguments but defined three parameters, likewise the area().
Here, rect.ar and rec1.ar in the above code are different.

Code:

>>> type(Rectangle.area )
<class 'function'>
>>> type(rec1.area)
<class 'method'>

Here, Rectangle.area is a function, and rec1.area is a method. Python has a unique feature that allows passing an object as the first argument in any function.

Here, rec1.area() and Rectangle.area(rec1) are similar

Commonly, the related class function gets called by putting the method's object before the first argument when the method gets called with some arguments.

That means obj.method(args) transforms to class.method(obj, args). That's why self ought to be the first parameter of instance methods in Python.

What is the difference between self and __init__?

  • Self: It is the instance of a class. With the help of self, one can access all the methods and attributes of a Python class.
  • __init__: It is a reserved method of Python classes. It is a constructor in OOP concepts. Whenever an object gets created from a class, __init__ gets called. It enables the class to initialize class attributes.

Conclusion:

The self parameter is a reference to the instance of a class. The reason Python programmers need to use self is that in Python we do not implement the @ syntax for referring to the instance attributes.

Accessing the instances through self is much faster and can smoothly work with the members associated with the class. It aids in accessing the variables which belong to a class. It can have other names instead of self.

It behaves as a keyword in programming languages except for Python. In Python, self is a convention that programmers follow, a parameter in a method definition.