In this post we’ll try to understand what is self in Python along with usage examples.
self variable
When you create a new instance of the class, for example-
obj = MyClass()
a new object is created with its own memory and reference to that object is assigned to the variable obj.
self in Python is also a reference to the current object and it is similar to this in Java and C++. By using self you can access the attributes and methods of a class in python.
Following Python example clarifies how self refers to the current object.
class Person: def __init__(self, name, age): self.name = name self.age = age def display_data(self): print(self.name) print(self.age) person1 = Person('John', 40) person1.display_data() person2 = Person('Jessica', 32) person2.display_data()
Output
John 40 Jessica 32
As you can see from the output when displayData()
method is called on the object person1 it displays the data person1 object is initialized with i.e. self is
referring to the person1 object. When displayData() method is called on the object person2 it displays the data person2 object is initialized with i.e. self
is referring to the person2 object.
Even in the __init__() function first argument is 'self'. When the constructor is called for initializing the object after it's creation object reference is passed to the first argument (self) of the constructor.
self is not a keyword in Python
All the methods in a class including __init__() must use self as the first parameter but naming this parameter "self" is more of a convention. In Python, self is not a reserved keyword, you can actually use any name as the first parameter of a method though that is not advisable.
Conventionally this parameter is called self in Python and that makes the code more readable.
Why self is not passed in method call
You would have observed in the above example that method call doesn’t include self as a parameter even though method definition does include self as parameter.
Method definition has self as parameter-
def display_data(self):
but when the method is called it is not passed-
person1.display_data()
To understand why method call still works you need to know a little bit about functions and methods in Python.
When you write any def statement with in a class it is a function definition which can be accessed using class name.
A method is a function that belongs to an object.
For example on printing the type of the following statements-
# method call using class name print(type(Person.display_data)) # method call using object print(type(person1.display_data))
Output is-
<class 'function'> <class 'method'>
Coming back to the question how method is called with out an argument. In Python when you call a method using an object, that instance object is passed as the first argument of the function. So a method call person1.display_data() internally becomes Person.display_data(person1) and that person1 reference is assigned to self.
That's all for this topic self in Python. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Python Tutorial Page
Related Topics
You may also like-
No comments:
Post a Comment