coding style - Using explicit del in python on local variables -


What are the best practices and recommendations for using the clear del statement in Python? I understand that it is used to remove attributes or dictionary / list elements, but occasionally I see that it is used on local variables in this type of code:

 < Code> def action (x): result = none = = sub-formation (x) if something is: qux = fu (some) result = bar (quax, some) del quax del some results  

Is there a serious reason for writing code?

Edit: Consider something "simple" without qux and some without the __ del __ method.

I do not remember when I finally used del - it is rare In fact, and usually this type of work is limited to such a code as a necessary import or after the choice to clean the namespace of the module.

Specifically, this is not no , as another (now-deleted) answer claims, that the use of

By del is the only way to ensure that the __ del __ method of an object is called

and it is very easy to understand is important. To help, let's make a class with a __ del __ and check when it's called:

  & gt; & Gt; & Gt; The shape of the square (object): ... def __del __ (self): print 'del', id (self) ... & gt; & Gt; & Gt; D = visdel ()> gt; & Gt; & Gt; A = list ()> gt; & Gt; & Gt; A.append (d)> and gt; & Gt; Dell D & gt; & Gt; & Gt;  

See? Del does not ensure that __ del __ is called: del removes a reference, and only the last < Reasons to call __ del __ are to be strong. So, also:

  & gt; & Gt; & Gt; A.append (visdel ()) & gt; & Gt; & Gt; A [:] = [1, 2, 3] del 550864 Dell 551184  

When the final reference is removed (which is not included in those methods including del then __ del __ - regardless of del Ever been involved in reducing the context of the object, or not, absolutely no differences.

Therefore, unless you specifically design a namespace (usually a namespace of a module, but imagine a class or example for a specific reason), from del Do not disturb (It may sometimes be easier to remove an item from the container, but I have found that I


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -