Is it safe to replace a self object by another object of the same type in a method?

It is unlikely that replacing the ‘self’ variable will accomplish whatever you’re trying to do, that couldn’t just be accomplished by storing the result of func(self) in a different variable. ‘self’ is effectively a local variable only defined for the duration of the method call, used to pass in the instance of the class which is being operated upon. Replacing self will not actually replace references to the original instance of the class held by other objects, nor will it create a lasting reference to the new instance which was assigned to it.

Leave a Comment