Repaso
EspaƱol
PythonCorePY-52

In a method def area(self):, what is self?

Options

  1. A reserved keyword that refers to the class itself
  2. The instance (object) the method was called on (Correct option)
  3. The parent class that the current class inherits from
  4. 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.

Key terms

Practice this question in Repaso