Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overwriting object instance does not release memory?

I have a python object which is essentially a collection of other object instances. You can append other objects to it (which it just stores in a list). It is created when reading a file, eg:

def file_reader(file):
    obj = MyCollection()

    for line in file:
        other_obj = line_reader(line)
        obj.append(other_obj)
    return obj

If I then try to overwrite the object later (by reading a different file), the original data is not deleted, the object is just extended. Strangely, this seems to happen if I use different references:

obj1 = file_reader(file)
obj2 = file_reader(file1)

I suspect I have some kind of problem with circular referencing, but I can't quite grasp the logic. Anyone have an idea?

Edit: The essential part of MyCollection looks like:

class MyCollection(object):

    def __init__(self, objs = []):
        self.objs = objs

   def append(self, obj):
        self.objs.append(obj)
like image 367
jramm Avatar asked Feb 14 '23 23:02

jramm


1 Answers

I'll take a not-so-wild guess that you currently have something like this:

class MyCollection():
    listOfThings = []
    def __init__(self):

In this case, listOfThings is a class attribute, it is shared amongst all instances of the MyCollection class. Instead, you want an instance attribute, separate for each instance:

class MyCollection():
    def __init__(self):
        self.listOfThings = []

Edit: so close!

The issue you have is in:

def __init__(self, objs = []):

Here, objs is known as a "mutable default parameter". The list in the default parameter values is shared for all instances of that class that don't pass anything to __init__ (defining a = MyCollection([]), for instance, would create a new instance with a separate objs list).

You should use:

def __init__(self, objs=None):
    if objs is None:
        objs = []

Canonical question covering this: "Least Astonishment" and the Mutable Default Argument

like image 93
jonrsharpe Avatar answered Mar 06 '23 06:03

jonrsharpe