Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Would you prefer using del or reassigning to None (garbage collecting)

Tags:

python

Consider following code:

if value and self.fps_display is None:     self.fps_display = clock.ClockDisplay() elif not value and self.fps_display is not None:     self.fps_display.unschedule()     # Do this     del self.fps_display     # or this     self.fps_display = None     # or leave both in ? 

Which is better python cleanup ?

like image 706
Red15 Avatar asked Jul 14 '11 13:07

Red15


People also ask

When should I use del in Python?

The del keyword is used to delete objects. In Python everything is an object, so the del keyword can also be used to delete variables, lists, or parts of a list etc.

Should you delete objects in Python?

1) Yes, I would recommend deleting the object. This will keep your code from getting bulky and/or slow. This is an especially good decision if you have a long run-time for your code, even though Python is pretty good about garbage collection.

What does Del return?

The del keyword doesn't return any value. The remove() method doesn't return any value. pop() returns deleted value. The del keyword can delete the single value from a list or delete the whole list at a time.

How do you collect garbage in Python?

Python's garbage collector runs during program execution and is triggered when an object's reference count reaches zero. An object's reference count changes as the number of aliases that point to it changes.


1 Answers

There is no difference for garbage collection — in both cases a reference to object pointed to by self.fps_display will be released. Which one you should use depends on whether you want the name to still exist (albeit now pointing to a different object, None), or not.

like image 156
Cat Plus Plus Avatar answered Sep 27 '22 15:09

Cat Plus Plus