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 ?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With