Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从模块调用函数中的函数_Python_Function_Unit Testing_Import_Module - Fatal编程技术网

Python 如何从模块调用函数中的函数

Python 如何从模块调用函数中的函数,python,function,unit-testing,import,module,Python,Function,Unit Testing,Import,Module,我试图对代码进行单元测试时遇到了问题。我有一个名为“compute_stats2.py”的脚本,它在函数“main”中包含一个已定义的函数“compute_stats”,因为我想稍后在main中构建一个命令行参数。compute_stats中使用的函数都是在main中定义的,但是为了整洁起见,我排除了它们,如果您需要查看,请告诉我。也就是说,我一直在尝试单元测试(请参阅下面的代码),每次尝试运行测试时,都会发生此错误 AttributeError:模块“compute\u stats2”没有属性

我试图对代码进行单元测试时遇到了问题。我有一个名为“compute_stats2.py”的脚本,它在函数“main”中包含一个已定义的函数“compute_stats”,因为我想稍后在main中构建一个命令行参数。compute_stats中使用的函数都是在main中定义的,但是为了整洁起见,我排除了它们,如果您需要查看,请告诉我。也就是说,我一直在尝试单元测试(请参阅下面的代码),每次尝试运行测试时,都会发生此错误

AttributeError:模块“compute\u stats2”没有属性“compute\u stats”

我想知道我是否在单元测试代码中没有正确调用函数。有没有关于如何更好地打电话的建议

p.S.提前道歉如果这是一个非常明显的修正,我不是最强大的编码员


计算_stats2.py
test_compute_stats2.py
compute\u stats()
main()
中的一个内部函数。因为你不在内部使用它,你可以把它拿出来

def main(): # can be removed if there is no other use
  pass


def compute_stats(values):
  if not values:
    return None
  else:
    o_min=minimum_of_list(values)
    o_max=values[-1]
    o_avg=average_of_list(values)
    o_median=median_of_list(values)
    return(o_min, o_max, o_avg, o_median)
顺便说一下,如果测试在类内,它们需要
self
作为第一个参数。您还应该将
列表
更改为参数名称,它是一个内置名称

def test_none_from_none(self, test_list):
compute\u stats()
main()
中的一个内部函数。因为你不在内部使用它,你可以把它拿出来

def main(): # can be removed if there is no other use
  pass


def compute_stats(values):
  if not values:
    return None
  else:
    o_min=minimum_of_list(values)
    o_max=values[-1]
    o_avg=average_of_list(values)
    o_median=median_of_list(values)
    return(o_min, o_max, o_avg, o_median)
顺便说一下,如果测试在类内,它们需要
self
作为第一个参数。您还应该将
列表
更改为参数名称,它是一个内置名称

def test_none_from_none(self, test_list):

您已经在
main
中构建了一个名为
compute\u stats
的本地函数。只有在由于作用域规则而调用
main
时,才会执行
compute\u stats
。因此,如果希望在当前状态下运行
compute\u stats
,则需要导入并运行main

如果要导入,最好将
compute\u stats
移到
main
之外。您将不再构建范围受main限制的本地函数

e、 g

def compute_stats(val):
通过
def main():
通过

您已经在
main
中构建了一个名为
compute\u stats
的本地函数。只有在由于作用域规则而调用
main
时,才会执行
compute\u stats
。因此,如果希望在当前状态下运行
compute\u stats
,则需要导入并运行main

如果要导入,最好将
compute\u stats
移到
main
之外。您将不再构建范围受main限制的本地函数

e、 g

def compute_stats(val):
通过
def main():
通过

严格来说,
compute\u stats
不是闭包,因为它不引用
main
范围内的任何名称。它只是一个局部函数。事实上,如果它是一个闭包,那么你建议的脱钩是不可能的(至少在没有对程序进行其他更改的情况下是不可能的)啊,是的,很好。谢谢你的更正!我编辑了我的措辞,删除了closure一词:)严格来说,
compute\u stats
不是一个闭包,因为它没有引用
main
范围内的任何名称。它只是一个局部函数。事实上,如果它是一个闭包,那么你建议的脱钩是不可能的(至少在没有对程序进行其他更改的情况下是不可能的)啊,是的,很好。谢谢你的更正!我已经编辑了我的措辞,删除了closure一词:)