Being a Python user, have you ever found the keyword (that actually is not a keyword) “self” just as a trap? Yes!!! But some of you might know its use but there are several beginners and other users who found ‘self’ a new word that is used everywhere in Python codes.
So, what is the purpose of the word ‘self’?
Before jumping to the details of the purpose of keyword self in Python and other useful details, let’s take a look at some useful details.
The self in Python is used to represent the class’s instance. But other programming languages like Ruby do not need it and the class’s instance can write as:
|class myScore |
@name = name
Whereas, Python needs to define the class as:
|class myScore: |
def mySubject(self, name):
self.name = name
“self” is written in 2 places. WHY? Do you need to define it in each line? Most importantly “what is the purpose of the word ‘self’ in Python.
Now, let’s find out the necessary details with relevant examples. Read the blog carefully to answer the MCQs mentioned at the end of the blog. The MCQs help you to test your knowledge and what you understand from this blog.
What is actually ‘self’ in Python?
As mentioned already, self represents the class’s instance. Using it, Python users access the attributes and class methods in Python. Moreover, the self can combine the attributes for the specified arguments.
Self is not a keyword but a convention. Moreover, self is a parameter in the instance method.
Why do Python users use self?
The basic reason to use self is that Python is not using the @ (at the rate) syntax for referring to an instance attribute. Apart from this, Python makes sure to do the things so that it can make the instance.
This instance consists of the methods that can be passed automatically but can not be received automatically. Or in others, we can say that “self always points to the current object.”
Let’s take an example of it:
The address of self is:
The address of the object is:
Let’s take another example of it:
(‘Animal is’, ‘dog’)
(‘Name is’, ‘Momo’)
(‘Animal is’, ‘cat’)
(‘Name is’, ‘Kitty’)
You can see that the self is the first argument that is passed in the Instance method or constructor.
Moreover, the self is provided as the primary parameter to the constructor and instance method. In case you forget to provide it, then it will raise an error.
Is it necessary to use self in each program in Python? If YES/NO!!! Then why?
No, it is not necessary to use self each time in a Python program.
You can easily name anything instead of using the self, the function of the parameter will not change. Moreover, it is advised to stick with the self because it is a convention.
I am using other parameters’ name at the place of self
A Brain Booster– Understanding what is the use of the word ‘self’?
Can self use to initiate variables in a class?
Yes, it can be used!!
Let’s take an example of it:
In this example, the self is used for referring to the variable name of the Person class. But if you use the variable within the method, it will not get executed.
Rather it will simply exist only during the method runs. In case you want to define the complete class variable, then you require defining them outside the class methods. This is the purpose of the word ‘self’.
Let’s wrap it up now!!!
It is quite clear that self in Python is used to point or represent the class’s instance that it was called. Moreover, it is a noticeable thing that the Python programming language does not have the @ syntax. In other programming languages, @ is used to class’s instance attributes.
This is what is the purpose of the word self in Python. We have explained the use of self with suitable examples. Try the given example to practice the self in Python.
Still have any doubt regarding ‘self’ in Python, let me know about it through your comments. We will help you in the best possible way.
Answer The Following Multiple Choice Questions To Know What You Have Studied
- The given statement is True or False:
“With the self, Python users can access the methods and attributes of the class.”
|Correct Answer: (A) This statement is true as self is used for accessing the methods and attributes of a class.|
- Is it not practicable to use self in variable initialization?
|Correct Answer: (A) Python users can use self for variable initialization also.|
- Which of these is the right method to determine an initializer method?
(A) __init__(self, title, author):
(B) def __init__():
(C) def __init__(self, title, author):
(D) def __init__(title, author):
|Correct Answer: (C) Self represents a class’s instance and the parameters can initialize the variables.|
- What will be the output of the code?
|Correct Answer: (D) As an object with age = 15 comes as a constructor, it is invoked to print 15. Then, the age changed to 16, which is why it prints the next statement that is 16.|
- Which of the following statement(s) is FALSE for the given code:
(A) name and age are known as parameters.
(B) Each class should have a constructor.
(C) “person1” is a reference variable for the object Person(“John”, 20)
(D) None of these
|Correct Answer: (B) It is not necessary that each class should have a constructor.|