Python del statement

The del statement doesn’t reclaim memory. It removes a reference, which decrements the reference count on the value. If the count is zero, the memory can be reclaimed. CPython will reclaim the memory immediately, there’s no need to wait for the garbage collector to run. In fact, the garbage collector is only needed for reclaiming … Read more

Python attributeError on __del__

Your __del__ method assumes that the class is still present by the time it is called. This assumption is incorrect. Groupclass has already been cleared when your Python program exits and is now set to None. Test if the global reference to the class still exists first: def __del__(self): if Groupclass: Groupclass.count -= 1 or … Read more

Delete an element from a dictionary

The del statement removes an element: del d[key] Note that this mutates the existing dictionary, so the contents of the dictionary changes for anybody else who has a reference to the same instance. To return a new dictionary, make a copy of the dictionary: def removekey(d, key): r = dict(d) del r[key] return r The … Read more