Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I overwrite an object in python? [duplicate]

Tags:

I have a program for which it would be most sensible to overwrite an object in memory instead of changing it, or redirecting a reference to it.

Basically, if I was writing C++, I would have multiple pointers all referring to the same address in memory. I would like to overwrite the contents at that address.

I would like to be able to do this from within the object itself as well.

i.e.

class MyClass
    def __init__(self):
        pass

    def overwrite(self,otherObjectOfTypeMyClass):
       #after this operation, all references to self now point to otherObjectOfTypeMyClass
like image 844
pixelpax Avatar asked Mar 27 '16 03:03

pixelpax


People also ask

Can you overwrite an object in Python?

In python what we call variables are really labels referring to objects. So if you want to change an object that is not immutable (like a list), that should work out-of-the-box in Python. However, you cannot really generally overwrite objects in Python. Creating a new object will use free memory.

Can you overwrite variables in Python?

In Python, a string is immutable. You cannot overwrite the values of immutable objects. However, you can assign the variable again. It's not modifying the string object; it's creating a new string object.

How do you duplicate an object in Python?

In Python, we use = operator to create a copy of an object. You may think that this creates a new object; it doesn't. It only creates a new variable that shares the reference of the original object.

How do you copy a list without changing the original Python?

deepcopy() Another key function in the copy module is the deepcopy() function. This function creates a completely independent copy of a list or any other compound object in Python.


2 Answers

Unlike C++, memory management is not under your control in Python. And in general, that is a good thing. Not having to worry about memory is one of the big benefits for those of us who started in C.

In python what we call variables are really labels referring to objects. So if you want to change an object that is not immutable (like a list), that should work out-of-the-box in Python.

However, you cannot really generally overwrite objects in Python. Creating a new object will use free memory. And memory that is in use will only be garbage-collected once it has no references to it.

like image 125
Roland Smith Avatar answered Oct 11 '22 13:10

Roland Smith


OK, I've found a solution that seems to work for my specific usage. I don't know the internals of python well enough to say whether this would break in unexpected ways. I would be interested in hearing why or why not this would be a dangerous thing to do. I'd also be interested if anyone has any ideas of scenarios in which this would not do what I'm expecting it to. I will wait to mark this correct for a few days in case anyone thinks that it's actually catastrophic.

def replaceobject(obj1,obj2):
    #Replaces obj1 with obj2
    obj1.__dict__ = obj2.__dict__


class MyClass:
    def __init__(self):
        self.attr0 = None
        self.attr1 = None
        self.attr2 = None
        self.attr3 = None

    def set_some_attrs(self):
        self.attr0 = 0
        self.attr1 = "1"
        self.attr2 = {"2":2}
        self.attr3 = [3]

#Set up scenario, three objects of the same type, one points to a different address
A = MyClass()
A.set_some_attrs()
C = A
B = MyClass()

#Make replacement and spit contents
replaceobject(B,A)
print B.attr0 #=> 0
print B.attr1 #=> "1"
print B.attr2 #=> {"2":2}
print B.attr3 #=> [3]

#Modify the replaced object and changes are reflected
B.attr2 = "made a change"
print A.attr2 #=> "made a change"
print C.attr2 #=> "made a change"
like image 45
pixelpax Avatar answered Oct 11 '22 14:10

pixelpax