When to use del - generic question

786
4
04-10-2012 01:30 PM
Zeke
by
Regular Contributor III
This is a generic type question, no specific problem involved.

What is good practice in deciding when to use del in a script? For example, I use it when done with a mapping.MapDocument object, but some of the Help files in the Resource Library seem to use it on variables as well. I don't see the point of that, but not a python expert either. Is there a guide to when you should use it? I looked at the Python documentation, but it was kind of obscure to me, especially when the documentation contains statements like "Rather than spelling it out in full details, here are some hints."
Tags (2)
0 Kudos
4 Replies
StephenBarrow
New Contributor
The del command deletes a reference to say a cursor and thus frees up some memory and more importantly releases the resource being used.

Consider a script where you open am updateCursor on a featureclass and thus lock it for others and your script runs for 20+ hours but only needs to update the featureclass and then does nothing with it for the remainder of the script, you have a featurecalss now ties up for 20+ hrs, if you use del and delete the reference to it and unlock it is is available for use then and users don't have to await the script's end.
Hope this helps

Stephen
0 Kudos
RyanForbes1
New Contributor III
In my experience I've only ever used del to delete a cursor after I was done with it, just to avoid any potential schema locks down the line in the script.  If I had to give a hard and fast rule, I would say to delete the cursor when you're done with it - it's better to be safe than sorry and recreate the cursor (say, at the start of a function) every time you need it and then delete it at the end of the function.  Otherwise, if I am just writing over a variable I never bother using del because reassignment works just fine.
0 Kudos
ChrisBater
New Contributor II
If you ever find yourself using big arrays you may find you need to delete them to free up memory.
0 Kudos
Zeke
by
Regular Contributor III
Thanks all.
0 Kudos