简体   繁体   English

不使用局部变量值 - python

[英]local variable value is not used - python

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: 我正在编写一些python代码(与ArcGIS结合使用),我有一个简单的语句运行良好,完全按照我的要求去做,我只是从我的脚本软件(PyCharm)得到一个“警告”我:

  • Local variable 'row' value is not used 不使用局部变量'row'值
  • 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 干杯

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? 请参阅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. 但正如Markus Meskanen指出的那样,有一种更好的方法来解决这个具体问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM