SelfNoExpression

Misconception:

In a method body, self is not an expression. It can only be used for the specific purpose of instance variable accesses and method calls/accesses.

Incorrect

The name self is not an expression

Correct

Even the name self on its own is an expression

Correction
Here is what's right.

self, the identifier of the first parameter in an instance method, is an expression that evaluates to an object reference. The type of self is the enclosing class.

class Count:
    def __init__(self, value):
        self.value = value

    def count_down_if_possible(self):
        if self.value > 0:
            return Count(self.value - 1)
        else:
            return self

Note that self is just an arbitrary name used as a convention for the first parameter in instance methods. The class instance is passed in the front of the argument list in the underlying definition of instance methods to provide a way to be accessed within the method.

Origin
Where could this misconception come from?

Students initially only see self being used to access instance variables (e.g., in the assignment self.x = 5). Thus, they develop the idea that self is a magical prefix with the sole purpose of qualifying instance variable accesses.

Symptoms
How do you know your students might have this misconception?

Students may believe that self on its own doesn’t have any meaning. They might express statements that treat pieces of code like return self or f(self) or x = self as wrong.

Value
How can you build on this misconception?

This misconception provides an opportunity to discuss the concept of expressions and to make students aware of the power of the idea of composing expressions from other expressions.

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.