【问题标题】:How do I list all functions for a Python module ignoring functions the module imports?如何列出 Python 模块的所有函数而忽略模块导入的函数?
【发布时间】:2020-12-06 09:50:03
【问题描述】:

我有一个 Python 模块“something.py”,我想获取我在此文件中编写的所有函数的列表。我关注了this SO 帖子,解释了执行此操作的标准方法。

问题是我在 something.py 中有导入函数,所以当我尝试列出我的函数时,我也会列出我不关心的导入函数。如何获取我在 something.py 中编写的函数列表?

【问题讨论】:

  • 您确定reference 中的答案无效吗?例如,在 adnan 的答案的 cmets 中,我们看到 from inspect import getmembers, isfunction; import something; functions_list = getmembers(something, isfunction) 仅提供了某个模块中的函数列表(即不包括导入函数)。
  • 不幸的是没有,我也对此感到惊讶。当我将我的导入注释掉时,它按预期工作,否则它会失败。我怀疑问题是我正在导入项目包中的所有内容,这可能导致它的行为与我只是尝试从标准库中导入某些内容不同。
  • @Visipi——这很令人惊讶,因为它可以在我的简单测试程序中工作,1)导入模块 something 和 2)something.py 模块导入 something_else 加上数学库。
  • 啊,谢谢你的理智测试。超越。我很确定现在问题出在我身上。我的 init 文件中可能有一些奇怪的东西。我会考虑回答,但我不知道如何给你点评论。
  • @Visipi--实际上在这种情况下,我认为 stackoverflow (SO) 认为这是一个不合适的问题,因为该问题无法重现(即其他人无法使用任何答案)。在这种情况下,我认为 SO 建议删除该问题,但我可能是错的。

标签: python import module python-import


【解决方案1】:
import <modulename>
dir(modulename)

第二行代码将给出模块中存在的函数列表

例如:

import math
dir(math)
['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']

【讨论】:

  • 是的,但是 dir() 还列出了该文件中的每个导入函数 例如,假设我将 numpy 导入到 something.py 中。当我打印 dir(something) 时,我对从 numpy 看到的东西不感兴趣。
【解决方案2】:

您可以使用 dir(yourModule) 返回属性列表:https://docs.python.org/3/library/functions.html#dir

【讨论】:

  • 但是 dir(yourModule) 也列出了我在 'yourModule.' 中导入的函数。我只对列出我编写的函数感兴趣。
  • 是的,你是对的,对此感到抱歉。我不确定这是否可能.. 你已经想通了吗?
【解决方案3】:

getmembers() 函数包含导入函数,因此您需要更多。一种想法是检查函数的模块名称。

from inspect import getmembers, isfunction
import something
import sys

mod = something

funcs = [
    f
    for _, f in getmembers(mod, isfunction)
    if f.__module__ == mod.__name__
]

for f in funcs:
    print(f)

或者您可以使用此条件进行过滤:

if sys.modules[f.__module__] is mod

无论哪种方式,输出都是:

<function bar at 0x107fb05f0>
<function foo at 0x107f840e0>

我的something 模块有这个:

import sys
from textwrap import dedent

fubb = 1

def foo():
    pass

def bar():
    pass

【讨论】:

    【解决方案4】:

    从 Python 2 到 3 的 this answer--Find functions explicitly defined in a module 更新函数(即将 itervalues() 替换为 values():

    def is_mod_function(mod, func):
        ' checks that func is a function defined in module mod '
        return inspect.isfunction(func) and inspect.getmodule(func) == mod
    
    
    def list_functions(mod):
        ' list of functions defined in module mod '
        return [func.__name__ for func in mod.__dict__.values() 
                if is_mod_function(mod, func)]
    

    用法

    print(list_functions(something))  # Output: ['a1', 'b1']
    

    文件 main.py(主模块)

    import inspect
    import something  # project module
    
    def is_mod_function(mod, func):
        return inspect.isfunction(func) and inspect.getmodule(func) == mod
    
    
    def list_functions(mod):
        return [func.__name__ for func in mod.__dict__.values() 
                if is_mod_function(mod, func)]
    
    def list_functions1(mod):
        return [func.__name__ for func in mod.__dict__.itervalues() 
                if is_mod_function(mod, func)]
    
    
    # Get list of functions defined only in module something
    print(list_functions(something))
    

    文件something.py(东西模块)

    from textwrap import dedent
    from math import sin, cos
    import numpy as np
    
    def a1():
      pass
    
    def b1():
      pass
    

    输出

    ['a1', 'b1']  # shows only the functions defined in something.py
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-13
      • 2022-11-11
      • 2011-04-06
      • 2013-07-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多