Observed
SelfAssignable

Incorrect
Reassigning self changes the object on which a method is called
Correct
Reassigning self does not change the object on which a method is called
CorrectionHere is what's right.
Here is what's right.
You cannot update an object by reassigning self
.
The first parameter of an instance method, conventionally named self
, is a reference to the object on which the method is called.
However, reassigning self
does not change the instance itself, as it is just a reference variable to the object instance.
Reassigning self
only changes what the local variable self
points to within the method’s scope.
For example:
class C:
def __init__(self):
self = 42
print(self)
obj = C() # displays 42
print(obj) # displays <__main__.C object at ...>
SymptomsHow do you know your students might have this misconception?
How do you know your students might have this misconception?
This misconception can manifest itself in code like the following:
class C:
def __init__(self):
self = C()
In this case, students may also be unaware of the fact that the expression C()
will result in infinite recursion (and stack overflow).
Language
Python