I would like to replace an object instance by another instance inside a method like this:
class A: def method1(self): self = func(self)
The object is retrieved from a database.
Advertisement
Answer
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.