In this article, let us look at what is a self in python class along with some examples to understand the self more clearly.
When we write a class in Python, we need a way to access its attributes and methods. Most languages have a fixed syntax for referring to attributes and methods; C++, for example, uses this for reference.
What is self in Python class?
The self in class is used to represent the instance of the class. In Python, you can access the attributes and methods of the class using this keyword. It binds the attributes with the arguments provided. We use self because Python does not use the ‘@’ syntax to refer to instance attributes. In Python, there are methods that cause the instance to be passed but not received automatically.
Self is always points to the Current Object
self in Python class
- Using EmailFilter
- Using self to call the methods
- Replacing self with some other name
- Without Providing self Parameter
Showing the address of self and object
Approach:
- Create a class say checkSelf.
- In the constructor, we print the value of self.
- Create an object for the above class (Here self referes to the this object)
- Print the id of the above created about using the id() function.
- The Exit of the Program.
Below is the implementation:
# Here we can observe that the self and rslt_object is referring to the same object # Create a class say checkSelf class checkSelf: # In the constructor we print the value of self def __init__(self): print("The Address/Id of self: ", id(self)) # Create an object for the above class (Here self referes to the this object) rslt_object = checkSelf() # Print the id of the above created about using the id() function print("The Address/Id of a checkSelf class object: ",id(rslt_object))
Output:
The Address/Id of self: 139973697862096 The Address/Id of a checkSelf class object: 139973697862096
Explanation:
Here we can observe that the self and rslt_object is referring to the same object. We can see the address of the self and the address of the object are the same.
Method #1: Using self to call the methods
Approach:
- Create a class say Employ
- Create a init constructor by passing self, employName, employId as arguments to it
- Initialize the instance variables
- Create a method say showEmployId which prints the employ id using self
- Create another method say showEmployName which prints the employ name using self
- Create a method say showEmployDetails which calls both the methods using self showEmployName(), showEmployId().
- Create an Instance/object of the above-created Employ class.
- Call the above method showEmployDetails() using the above employ Object.
- The Exit of the Program.
Below is the implementation:
# Create a class say Employ class Employ(): # Create a init constructor by passing self, employName, employId as arguments to it def __init__ (self, employName, employId): # Initilaize the instance variables self.employName = employName self.employId = employId # Create a method say showEmployId which prints the employ id using self def showEmployId(self): print("Employ Id = ", self.employId) # Create another method say showEmployName which prints the employ name using self def showEmployName(self): print("Employ Name = ", self.employName) # Create a method say showEmployDetails which calls both the methods using self # showEmployName, showEmployId def showEmployDetails(self): self.showEmployName() self.showEmployId() # Create an Instance/object of the above created Employ class employObj = Employ("John", 2122) # Call the above method showEmployDetails() using the above employ Object employObj.showEmployDetails()
Output:
Employ Name = John Employ Id = 2122
Explanation:
- Here all the methods of the class have self as the first parameter.
- The self is used to call the attributes employName and employId.
- Here, the self is used to call the methods showEmployId() and showEmployName().
NOTE
The term self is not a keyword; rather, it is a Python developer convention. You can substitute any other word for the first parameter and it will still work. However, it is preferable to use self rather than anything else because it enhances code readability.
Method #2: Replacing self with some other name
In function, self is a parameter that can be replaced with a different parameter name.
Here we replace self with hello in the above example. Still, it works fine.
Approach:
- Create a class say Employ
- Create a init constructor by passing employName, employId as arguments to it using hello instead of self
- Initilaize the instance variables
- Create a function say showEmployId which prints the employ id using hello instead of self
- Create another function say showEmployName which prints the employ name using hello instead of self
- Create a function say showEmployDetails which calls both the functions showEmployName(), showEmployId() using hello instead of self.
- Create an Instance/object of the above created Employ class
- Call the above function showEmployDetails() using the above employ Object.
- The Exit of the Program.
Below is the implementation:
# Create a class say Employ class Employ(): # Create a init constructor by passing employName, employId as # arguments to it using hello instead of self def __init__ (hello, employName, employId): # Initilaize the instance variables hello.employName = employName hello.employId = employId # Create a function say showEmployId which prints the employ id # using hello instead of self def showEmployId(hello): print("Employ Id = ", hello.employId) # Create another function say showEmployName which prints the employ name # using hello instead of self def showEmployName(hello): print("Employ Name = ", hello.employName) # Create a function say showEmployDetails which calls both the functions # showEmployName, showEmployId using hello instead of self def showEmployDetails(hello): hello.showEmployName() hello.showEmployId() # Create an Instance/object of the above created Employ class employObj = Employ("John", 2122) # Call the above function showEmployDetails() using the above employ Object employObj.showEmployDetails()
Output:
Employ Name = John Employ Id = 2122
Method #3: Without Providing self Parameter
The Instance method and constructor, self must be provided as a First parameter. It will result in an error if you do not provide it.
# Here we can observe that the self is not passed as the first parameter # in the init constructor. Hence it throws an error # Create a class say checkwithoutSelf class checkwithoutSelf: # In the init constructor we don't pass the self def __init__(): print("hello this is python programs") # Create an object for the above class rslt_object = checkwithoutSelf() print("Welcome")
Output:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-43-394e0a56a3d8> in <module> 9 10 # Create an object for the above class ---> 11 rslt_object = checkwithoutSelf() 12 print("Welcome") TypeError: __init__() takes 0 positional arguments but 1 was given