All instances of a class share the same doc string . Well, here's the result. In Python, simply call a class as if it were a function to create a new instance of the class. If creating new instances is easy, destroying them is even easier. In general, there is no need to explicitly free instances, because they are freed automatically when the variables assigned to them go out of scope.
Memory leaks are rare in Python.
Every time the leakmem function is called, we are creating an instance of FileInfo and assigning it to the variable f , which is a local variable within the function. Then the function ends without ever freeing f , so you would expect a memory leak, but you would be wrong. When the function ends, the local variable f goes out of scope. No matter how many times we call the leakmem function, it will never leak memory, because every time, Python will destroy the newly created FileInfo class before returning from leakmem . Python keeps a list of references to every instance created.