python - 明确定义方法时出现 NameError
问题描述
我在 python 中有一个非常简单的程序来检查列表中是否有零值
class ZeroList(object):
def __init__(self,coordinates):
self.coordinates = coordinates
def is_zero(value):
return math.isclose(value,0)
def test(self):
for coord in self.coordinate:
if not is_zero(coord):
print(coord)
a = ZeroList([1,2,0,8,5,0,3,7,0,3])
a.test()
然而运行这会导致错误
Traceback (most recent call last):
File "line2.py", line 2, in <module>
class Test(object):
File "line2.py", line 12, in Test
test([1,2,3])
File "line2.py", line 8, in test
if not is_zero(i):
NameError: name 'is_zero' is not defined
方法 is_zero 已明确定义,为什么会出现此错误?
解决方案
推荐阅读
- node.js - 无法使用 Angular 6 打印 Node.js 数据
- javascript - 将骰子值附加到动态 javascript 制作的 html 代码
- amazon-mws - 检查产品是FBA还是FBM
- vba - 写入单元格会写入整个范围
- google-cloud-platform - 是否可以从 Google Compute Engine 虚拟服务器使用 Amazon SQS?
- react-native - FlatList 中的样式
- java - 下面的代码会执行成功吗?
- docker - 在詹金斯中推送标记的图像
- c# - 评估列表中的项目是否具有满足条件的属性
- c# - 在查询中隐式包含所有相关数据