Whether it's absolutely necessary is the wrong question to ask. The question is whether it's a good idea.
As a rule in programming, you should avoid doing weird things and use the best tool for the job. If something has an explicit way of releasing resources, just make the release explicit and be done with it:
with arcpy.da.UpdateCursor(fc,fields) as cursor:
d = {k: v for (k,v) in cursor}
What you may not be aware of is that the with
clause actually invokes additional logic. A with
clause requires a context manager, which must have an __enter__
(invoked when the block is entered) and __exit__
(invoked when the block is exited) method. In particular, the __exit__
method is invoked regardless of whether an exception occurred, ensuring the program always releases the resource even on error. This gives your code explicit documentation of when a resource is acquired and when it is released, and it ensures that a resource can be released as soon as possible.
By contrast, you can't actually depend on the runtime to magically close it immediately for you. This is because the way it gets closed is by invoking the object's destructor, which may or may not happen immediately. Python does not make any guarantees about when a destructor is invoked, only that it will be eventually when the object is garbage collected. (See here.) Currently, Python is implemented so that it happens as soon as soon as there's no longer a reference to an object. But it's easy to accidentally propagate references to an object, and Python's runtime might change.
Also consider the long term maintenance. There's no long term reference to it now, but what happens in 6 months when you need to modify the code so that there is a reference? What if someone else does it? The person making the change may not think to switch to a with
block since there's not one already there. Make cleaning up your resources a habit, and you'll have far fewer problems with it.
Do you really want to tie your code to implementation details of garbage collection? Do you want to have to constantly think about whether you might be accidentally propagating a reference via an exception? No, you don't. Imagine if that happened when the script was invoked in ArcMap. The user would be forced to close the entire process just to release the file. So don't put yourself in that position. Release the resource explicitly. Saving one line of code is not worth the risks of problems it can cause. Context managers are the standard mechanism for acquiring and releasing resources in Python, and they do it very well.
The bottom line is that not releasing it explicitly is a bad idea.
This, of course, assumes that the code has some possibility of affecting someone else, like putting it in a script that someone else will need to run or maintain or it might delay delivering your work if you have to close ArcMap all the way because you can't save your changes. If you're the only one who will be impacted by a problem, then by all means, fly in the face of good practices all you want.
da
cursors: sgillies.net/2011/02/01/get-with-it.html and help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//…. In particular, look at @JasonScheirer 's comments at the bottom of the first link.