Is it necessary to delete a cursor used in a comprehension?
up vote
6
down vote
favorite
If it is best to open cursors using a with statement to ensure it is deleted, like so:
with arcpy.da.UpdateCursor(fc,fields) as cursor:
Then, if a cursor is used as the iterable in a comprehension like so:
d = {k:v for (k,v) in arcpy.da.SearchCursor(fc,fields)}
Is it necessary to delete the cursor after using it in the comprehension?
arcpy python cursor
add a comment |
up vote
6
down vote
favorite
If it is best to open cursors using a with statement to ensure it is deleted, like so:
with arcpy.da.UpdateCursor(fc,fields) as cursor:
Then, if a cursor is used as the iterable in a comprehension like so:
d = {k:v for (k,v) in arcpy.da.SearchCursor(fc,fields)}
Is it necessary to delete the cursor after using it in the comprehension?
arcpy python cursor
1
Great question. Are you trying to handle schema locks? There are some early (mostly outdated) posts on a similar topic, although I cannot find a definitive source on the newda
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.
– Aaron♦
8 hours ago
add a comment |
up vote
6
down vote
favorite
up vote
6
down vote
favorite
If it is best to open cursors using a with statement to ensure it is deleted, like so:
with arcpy.da.UpdateCursor(fc,fields) as cursor:
Then, if a cursor is used as the iterable in a comprehension like so:
d = {k:v for (k,v) in arcpy.da.SearchCursor(fc,fields)}
Is it necessary to delete the cursor after using it in the comprehension?
arcpy python cursor
If it is best to open cursors using a with statement to ensure it is deleted, like so:
with arcpy.da.UpdateCursor(fc,fields) as cursor:
Then, if a cursor is used as the iterable in a comprehension like so:
d = {k:v for (k,v) in arcpy.da.SearchCursor(fc,fields)}
Is it necessary to delete the cursor after using it in the comprehension?
arcpy python cursor
arcpy python cursor
asked 8 hours ago
J. Flann
585
585
1
Great question. Are you trying to handle schema locks? There are some early (mostly outdated) posts on a similar topic, although I cannot find a definitive source on the newda
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.
– Aaron♦
8 hours ago
add a comment |
1
Great question. Are you trying to handle schema locks? There are some early (mostly outdated) posts on a similar topic, although I cannot find a definitive source on the newda
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.
– Aaron♦
8 hours ago
1
1
Great question. Are you trying to handle schema locks? There are some early (mostly outdated) posts on a similar topic, although I cannot find a definitive source on the new
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.– Aaron♦
8 hours ago
Great question. Are you trying to handle schema locks? There are some early (mostly outdated) posts on a similar topic, although I cannot find a definitive source on the new
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.– Aaron♦
8 hours ago
add a comment |
3 Answers
3
active
oldest
votes
up vote
3
down vote
accepted
No, it is not necessary to delete a cursor
after using it in a comprehension. A cursor
is an instance of a class, which is an object (everything in python is an object). Every python session has a namespace
which contains references to all the objects in the session - think of it like a dictionary where the keys are references to each object, and the values are the objects themselves. When the 'reference count' - the number of keys that refer to that object - drops to zero, the object is removed and the memory re-allocated. When you use a cursor
in a comprehension, there is no reference to that object in the namespace. After the comprehension is done, the object will be deleted.
There is no entry in the namespace, and therefore no need to delete anything. ESRI also illustrates this syntax in example 2, here.
To further clarify, if you run:
>>> import arcpy
>>> f = r'C:Workspacestudy_area.shp'
>>> a = arcpy.da.SearchCursor(f, ['*'])
You will see a .lock file appear in the directory (check your file explorer). The reference to the cursor is a
, which will make the cursor
(and therefore the lock) persist until a
is deleted. So when you then run:
>>> del(a)
The entry in the namespace will be removed, and the lock will release (.lock file will disappear). If you run:
>>> t = [i for i in arcpy.da.SearchCursor(f, ['*'])]
You either won't see a lock file, or it will disappear when the command completes. Without an entry in the namespace, the cursor
is not persistent. t
refers to the list you just created, not the cursor
used to create it.
To summarize, you need only worry about deleting cursors
when they have a reference in the namespace (i.e. when you have assigned them to a variable, like a
in the above example).
This is extremely poor programming practice. If something has an explicit way of releasing resources, you use it.
– jpmc26
1 hour ago
add a comment |
up vote
0
down vote
Update and insert cursors cannot be created for a table or feature class if an exclusive lock exists for that dataset. The UpdateCursor
or InsertCursor functions fail because of an exclusive lock on the
dataset. If these functions successfully create a cursor, they apply
an exclusive lock on the dataset so that two scripts cannot create an
update or insert cursor on the same dataset.
In Python, the lock persists until the cursor is released. Otherwise,
all other applications or scripts could be unnecessarily prevented
from accessing a dataset. A cursor can released by one of the
following:
Including the cursor inside a with statement, which will guarantee the
release of locks regardless of whether or not the cursor is
successfully completed Calling reset() on the cursor The completion of
the cursor Explicitly deleting the cursor using Python's del statement
- ESRI
Locking with arcpy.da cursors is pretty much the same as locking with the original arcpy cursors.
After testing your code, and as gberard pointed out, there is no reference to the cursor after the comprehension ends.
Also, there are no locks on the feature class after the comprehension ends. Search cursor does not lock the feature class anyway, so there is no need to worry about the cursor after the comprehension.
Deleting what? There's no reference to the cursor object after the comprehension ends, so in theory it should get closed. Whether or not ESRI's implementation behaves as you'd expect is another question, and I don't think the docs really answer that.
– gberard
6 hours ago
add a comment |
up vote
0
down vote
Whether it's absolutely necessary is the wrong question to ask. The question is whether you should. As a rule in programming, when it comes to releasing resources, 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? 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. No, you don't. 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.
This, of course, assumes you're putting this code in a script to be run and potentially maintained by someone else. 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.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
No, it is not necessary to delete a cursor
after using it in a comprehension. A cursor
is an instance of a class, which is an object (everything in python is an object). Every python session has a namespace
which contains references to all the objects in the session - think of it like a dictionary where the keys are references to each object, and the values are the objects themselves. When the 'reference count' - the number of keys that refer to that object - drops to zero, the object is removed and the memory re-allocated. When you use a cursor
in a comprehension, there is no reference to that object in the namespace. After the comprehension is done, the object will be deleted.
There is no entry in the namespace, and therefore no need to delete anything. ESRI also illustrates this syntax in example 2, here.
To further clarify, if you run:
>>> import arcpy
>>> f = r'C:Workspacestudy_area.shp'
>>> a = arcpy.da.SearchCursor(f, ['*'])
You will see a .lock file appear in the directory (check your file explorer). The reference to the cursor is a
, which will make the cursor
(and therefore the lock) persist until a
is deleted. So when you then run:
>>> del(a)
The entry in the namespace will be removed, and the lock will release (.lock file will disappear). If you run:
>>> t = [i for i in arcpy.da.SearchCursor(f, ['*'])]
You either won't see a lock file, or it will disappear when the command completes. Without an entry in the namespace, the cursor
is not persistent. t
refers to the list you just created, not the cursor
used to create it.
To summarize, you need only worry about deleting cursors
when they have a reference in the namespace (i.e. when you have assigned them to a variable, like a
in the above example).
This is extremely poor programming practice. If something has an explicit way of releasing resources, you use it.
– jpmc26
1 hour ago
add a comment |
up vote
3
down vote
accepted
No, it is not necessary to delete a cursor
after using it in a comprehension. A cursor
is an instance of a class, which is an object (everything in python is an object). Every python session has a namespace
which contains references to all the objects in the session - think of it like a dictionary where the keys are references to each object, and the values are the objects themselves. When the 'reference count' - the number of keys that refer to that object - drops to zero, the object is removed and the memory re-allocated. When you use a cursor
in a comprehension, there is no reference to that object in the namespace. After the comprehension is done, the object will be deleted.
There is no entry in the namespace, and therefore no need to delete anything. ESRI also illustrates this syntax in example 2, here.
To further clarify, if you run:
>>> import arcpy
>>> f = r'C:Workspacestudy_area.shp'
>>> a = arcpy.da.SearchCursor(f, ['*'])
You will see a .lock file appear in the directory (check your file explorer). The reference to the cursor is a
, which will make the cursor
(and therefore the lock) persist until a
is deleted. So when you then run:
>>> del(a)
The entry in the namespace will be removed, and the lock will release (.lock file will disappear). If you run:
>>> t = [i for i in arcpy.da.SearchCursor(f, ['*'])]
You either won't see a lock file, or it will disappear when the command completes. Without an entry in the namespace, the cursor
is not persistent. t
refers to the list you just created, not the cursor
used to create it.
To summarize, you need only worry about deleting cursors
when they have a reference in the namespace (i.e. when you have assigned them to a variable, like a
in the above example).
This is extremely poor programming practice. If something has an explicit way of releasing resources, you use it.
– jpmc26
1 hour ago
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
No, it is not necessary to delete a cursor
after using it in a comprehension. A cursor
is an instance of a class, which is an object (everything in python is an object). Every python session has a namespace
which contains references to all the objects in the session - think of it like a dictionary where the keys are references to each object, and the values are the objects themselves. When the 'reference count' - the number of keys that refer to that object - drops to zero, the object is removed and the memory re-allocated. When you use a cursor
in a comprehension, there is no reference to that object in the namespace. After the comprehension is done, the object will be deleted.
There is no entry in the namespace, and therefore no need to delete anything. ESRI also illustrates this syntax in example 2, here.
To further clarify, if you run:
>>> import arcpy
>>> f = r'C:Workspacestudy_area.shp'
>>> a = arcpy.da.SearchCursor(f, ['*'])
You will see a .lock file appear in the directory (check your file explorer). The reference to the cursor is a
, which will make the cursor
(and therefore the lock) persist until a
is deleted. So when you then run:
>>> del(a)
The entry in the namespace will be removed, and the lock will release (.lock file will disappear). If you run:
>>> t = [i for i in arcpy.da.SearchCursor(f, ['*'])]
You either won't see a lock file, or it will disappear when the command completes. Without an entry in the namespace, the cursor
is not persistent. t
refers to the list you just created, not the cursor
used to create it.
To summarize, you need only worry about deleting cursors
when they have a reference in the namespace (i.e. when you have assigned them to a variable, like a
in the above example).
No, it is not necessary to delete a cursor
after using it in a comprehension. A cursor
is an instance of a class, which is an object (everything in python is an object). Every python session has a namespace
which contains references to all the objects in the session - think of it like a dictionary where the keys are references to each object, and the values are the objects themselves. When the 'reference count' - the number of keys that refer to that object - drops to zero, the object is removed and the memory re-allocated. When you use a cursor
in a comprehension, there is no reference to that object in the namespace. After the comprehension is done, the object will be deleted.
There is no entry in the namespace, and therefore no need to delete anything. ESRI also illustrates this syntax in example 2, here.
To further clarify, if you run:
>>> import arcpy
>>> f = r'C:Workspacestudy_area.shp'
>>> a = arcpy.da.SearchCursor(f, ['*'])
You will see a .lock file appear in the directory (check your file explorer). The reference to the cursor is a
, which will make the cursor
(and therefore the lock) persist until a
is deleted. So when you then run:
>>> del(a)
The entry in the namespace will be removed, and the lock will release (.lock file will disappear). If you run:
>>> t = [i for i in arcpy.da.SearchCursor(f, ['*'])]
You either won't see a lock file, or it will disappear when the command completes. Without an entry in the namespace, the cursor
is not persistent. t
refers to the list you just created, not the cursor
used to create it.
To summarize, you need only worry about deleting cursors
when they have a reference in the namespace (i.e. when you have assigned them to a variable, like a
in the above example).
edited 3 hours ago
answered 6 hours ago
Chris
623214
623214
This is extremely poor programming practice. If something has an explicit way of releasing resources, you use it.
– jpmc26
1 hour ago
add a comment |
This is extremely poor programming practice. If something has an explicit way of releasing resources, you use it.
– jpmc26
1 hour ago
This is extremely poor programming practice. If something has an explicit way of releasing resources, you use it.
– jpmc26
1 hour ago
This is extremely poor programming practice. If something has an explicit way of releasing resources, you use it.
– jpmc26
1 hour ago
add a comment |
up vote
0
down vote
Update and insert cursors cannot be created for a table or feature class if an exclusive lock exists for that dataset. The UpdateCursor
or InsertCursor functions fail because of an exclusive lock on the
dataset. If these functions successfully create a cursor, they apply
an exclusive lock on the dataset so that two scripts cannot create an
update or insert cursor on the same dataset.
In Python, the lock persists until the cursor is released. Otherwise,
all other applications or scripts could be unnecessarily prevented
from accessing a dataset. A cursor can released by one of the
following:
Including the cursor inside a with statement, which will guarantee the
release of locks regardless of whether or not the cursor is
successfully completed Calling reset() on the cursor The completion of
the cursor Explicitly deleting the cursor using Python's del statement
- ESRI
Locking with arcpy.da cursors is pretty much the same as locking with the original arcpy cursors.
After testing your code, and as gberard pointed out, there is no reference to the cursor after the comprehension ends.
Also, there are no locks on the feature class after the comprehension ends. Search cursor does not lock the feature class anyway, so there is no need to worry about the cursor after the comprehension.
Deleting what? There's no reference to the cursor object after the comprehension ends, so in theory it should get closed. Whether or not ESRI's implementation behaves as you'd expect is another question, and I don't think the docs really answer that.
– gberard
6 hours ago
add a comment |
up vote
0
down vote
Update and insert cursors cannot be created for a table or feature class if an exclusive lock exists for that dataset. The UpdateCursor
or InsertCursor functions fail because of an exclusive lock on the
dataset. If these functions successfully create a cursor, they apply
an exclusive lock on the dataset so that two scripts cannot create an
update or insert cursor on the same dataset.
In Python, the lock persists until the cursor is released. Otherwise,
all other applications or scripts could be unnecessarily prevented
from accessing a dataset. A cursor can released by one of the
following:
Including the cursor inside a with statement, which will guarantee the
release of locks regardless of whether or not the cursor is
successfully completed Calling reset() on the cursor The completion of
the cursor Explicitly deleting the cursor using Python's del statement
- ESRI
Locking with arcpy.da cursors is pretty much the same as locking with the original arcpy cursors.
After testing your code, and as gberard pointed out, there is no reference to the cursor after the comprehension ends.
Also, there are no locks on the feature class after the comprehension ends. Search cursor does not lock the feature class anyway, so there is no need to worry about the cursor after the comprehension.
Deleting what? There's no reference to the cursor object after the comprehension ends, so in theory it should get closed. Whether or not ESRI's implementation behaves as you'd expect is another question, and I don't think the docs really answer that.
– gberard
6 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
Update and insert cursors cannot be created for a table or feature class if an exclusive lock exists for that dataset. The UpdateCursor
or InsertCursor functions fail because of an exclusive lock on the
dataset. If these functions successfully create a cursor, they apply
an exclusive lock on the dataset so that two scripts cannot create an
update or insert cursor on the same dataset.
In Python, the lock persists until the cursor is released. Otherwise,
all other applications or scripts could be unnecessarily prevented
from accessing a dataset. A cursor can released by one of the
following:
Including the cursor inside a with statement, which will guarantee the
release of locks regardless of whether or not the cursor is
successfully completed Calling reset() on the cursor The completion of
the cursor Explicitly deleting the cursor using Python's del statement
- ESRI
Locking with arcpy.da cursors is pretty much the same as locking with the original arcpy cursors.
After testing your code, and as gberard pointed out, there is no reference to the cursor after the comprehension ends.
Also, there are no locks on the feature class after the comprehension ends. Search cursor does not lock the feature class anyway, so there is no need to worry about the cursor after the comprehension.
Update and insert cursors cannot be created for a table or feature class if an exclusive lock exists for that dataset. The UpdateCursor
or InsertCursor functions fail because of an exclusive lock on the
dataset. If these functions successfully create a cursor, they apply
an exclusive lock on the dataset so that two scripts cannot create an
update or insert cursor on the same dataset.
In Python, the lock persists until the cursor is released. Otherwise,
all other applications or scripts could be unnecessarily prevented
from accessing a dataset. A cursor can released by one of the
following:
Including the cursor inside a with statement, which will guarantee the
release of locks regardless of whether or not the cursor is
successfully completed Calling reset() on the cursor The completion of
the cursor Explicitly deleting the cursor using Python's del statement
- ESRI
Locking with arcpy.da cursors is pretty much the same as locking with the original arcpy cursors.
After testing your code, and as gberard pointed out, there is no reference to the cursor after the comprehension ends.
Also, there are no locks on the feature class after the comprehension ends. Search cursor does not lock the feature class anyway, so there is no need to worry about the cursor after the comprehension.
edited 6 hours ago
answered 7 hours ago
jbalk
4,054728
4,054728
Deleting what? There's no reference to the cursor object after the comprehension ends, so in theory it should get closed. Whether or not ESRI's implementation behaves as you'd expect is another question, and I don't think the docs really answer that.
– gberard
6 hours ago
add a comment |
Deleting what? There's no reference to the cursor object after the comprehension ends, so in theory it should get closed. Whether or not ESRI's implementation behaves as you'd expect is another question, and I don't think the docs really answer that.
– gberard
6 hours ago
Deleting what? There's no reference to the cursor object after the comprehension ends, so in theory it should get closed. Whether or not ESRI's implementation behaves as you'd expect is another question, and I don't think the docs really answer that.
– gberard
6 hours ago
Deleting what? There's no reference to the cursor object after the comprehension ends, so in theory it should get closed. Whether or not ESRI's implementation behaves as you'd expect is another question, and I don't think the docs really answer that.
– gberard
6 hours ago
add a comment |
up vote
0
down vote
Whether it's absolutely necessary is the wrong question to ask. The question is whether you should. As a rule in programming, when it comes to releasing resources, 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? 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. No, you don't. 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.
This, of course, assumes you're putting this code in a script to be run and potentially maintained by someone else. 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.
add a comment |
up vote
0
down vote
Whether it's absolutely necessary is the wrong question to ask. The question is whether you should. As a rule in programming, when it comes to releasing resources, 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? 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. No, you don't. 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.
This, of course, assumes you're putting this code in a script to be run and potentially maintained by someone else. 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.
add a comment |
up vote
0
down vote
up vote
0
down vote
Whether it's absolutely necessary is the wrong question to ask. The question is whether you should. As a rule in programming, when it comes to releasing resources, 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? 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. No, you don't. 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.
This, of course, assumes you're putting this code in a script to be run and potentially maintained by someone else. 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.
Whether it's absolutely necessary is the wrong question to ask. The question is whether you should. As a rule in programming, when it comes to releasing resources, 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? 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. No, you don't. 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.
This, of course, assumes you're putting this code in a script to be run and potentially maintained by someone else. 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.
edited 49 mins ago
answered 1 hour ago
jpmc26
827721
827721
add a comment |
add a comment |
Thanks for contributing an answer to Geographic Information Systems Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f305867%2fis-it-necessary-to-delete-a-cursor-used-in-a-comprehension%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Great question. Are you trying to handle schema locks? There are some early (mostly outdated) posts on a similar topic, although I cannot find a definitive source on the new
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.– Aaron♦
8 hours ago