In a method def area(self):, what is self?
Options
- A reserved keyword that refers to the class itself
- The instance (object) the method was called on (Correct option)
- The parent class that the current class inherits from
- A global variable that every method can read
Answer
The instance (object) the method was called on
Why
When you write rect.area(), Python actually calls Rectangle.area(rect): the object before the dot is passed automatically as the first argument.
By convention that first parameter is called self. Through it, the method reads and changes that object's data, like self.width.
Remember
self = the object the method is working on.