<p>我有以下代码</p>
<pre><code>fset = [ obj for name,obj in inspect.getmembers(sys.modules[__name__]) if inspect.isfunction(obj) ]
def func(num):
pass
if __name__ == "__main__":
print(fset)
</code></pre>
<p>印刷品</p>
<pre><code>[]
</code></pre>
<p>然而</p>
<pre><code>def func(num):
pass
fset = [ obj for name,obj in inspect.getmembers(sys.modules[__name__]) if inspect.isfunction(obj) ]
if __name__ == "__main__":
print(fset)
</code></pre>
<p>印刷品</p>
<pre><code>[<function func at 0x7f35c29383b0>]
</code></pre>
<p>那么,fset是当前模块中所有函数的列表,其中fset是在所有函数的顶部定义的?</p>
<p>编辑一:我想做的是</p>
<pre><code>def testall(arg):
return any(f(arg) for f in testfunctions)
def test1(arg):
#code here
# may call testall but wont call anyother test*
def test2(arg):
#code here
# may call testall but wont call anyother test*
</code></pre>
<p>今后可能会增加更多的测试功能。所以这就是fset/testfunctions的原因</p>