I used the following stack class in Python to store objects of another class.
class Stack :
def __init__(self) :
self.items = []
def push(self, item) :
self.items.append(item)
def pop(self) :
return self.items.pop()
def isEmpty(self) :
return (self.items == [])
scopeStack=Stack();
object1=AnotherClass();
object1.value=2;
scopeStack.push(object1);
On changing the contents of the object object1 outside the stack, the contents of the stack’s object changed too.
object1.value=3;
obj=scopeStack.pop();
print obj.value; #gives output 3
What should I do to NOT have this dynamic binding between a local variable and the stack’s inner variables?
Check out the
copymodule found here. What you are looking for is calledcopy.deepcopy().Example:
Output: