[英]Local variable 'line' value is not used?
When this script is used without the 'def main():' then the error doesn't appear but when it is inside this function the error seems to appear. 如果在不使用'def main():'的情况下使用此脚本,则不会出现该错误,但是在该函数内部时,该错误似乎会出现。
I have had a look into what I could find online regarding this issue but none of them can directly relate. 我调查了我在网上可以找到的有关此问题的信息,但没有一个可以直接关联。
Sorry had to fix the code 对不起,必须修复代码
def main():
count = sum(1 for line in open('text.txt'))
Note that this isn't an error, it's a warning. 请注意,这不是错误,而是警告。 You can ignore it if you want to, but personally I also like removing warnings.
如果愿意,您可以忽略它,但是我个人也喜欢删除警告。
The conventional way to signal to both readers and tools that a variable is intentionally unused is to name it _
(a single underscore), eg 向读取器和工具发出信号通知有意未使用变量的常规方法是将其命名为
_
(单个下划线),例如
def main():
count = sum(1 for _ in open('text.txt'))
In PyCharm, prefixing with an underscore (eg _line
) also works if you want readers to know what the ignored variable is. 在PyCharm中,如果您希望读者知道什么是被忽略的变量,则在下划线前面添加前缀(例如
_line
)也可以。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.