local variable value is not used - python

6.5k views Asked by At

I am writing some python code (to work in conjuction with ArcGIS), and I have a simple statement that runs fine and does exactly what I am asking it to do, I just get a 'warning' from my scripting software (PyCharm) telling me:

  • Local variable 'row' value is not used
  • This inspection highlights local variables, parameters or local functions unused in scope.

I understand it is not used, because it is not needed. This is the only way (that I know of personally) to work out how many rows exist in a table.

Can someone tell me if there is a better (more correct) way of writing this??

cursor = arcpy.SearchCursor(my_table)
for row in cursor:
    count += 1
print count

Cheers

1

There are 1 answers

5
Eric Renouf On BEST ANSWER

By convention if you're looping and don't intend to use the value you store the iterator in a variable named _. This is still a normal variable that gets each value in turn, but is taken to mean "I don't plan to use this value." To use this convention you'd rewrite your code as:

cursor = arcpy.SearchCursor(my_table)
for _ in cursor:
    count += 1
print count

See What is the purpose of the single underscore "_" variable in Python? to learn more about the single underscore variable.

But as Markus Meskanen pointed out there is a better way to solve this specific problem.