获取模块的所有功能 [英] Get all functions of a module

查看:113
本文介绍了获取模块的所有功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个项目具有以下结构:

A project has the following structure:

modulename
├── __init__.py
│
├── one
│   ├── function_1.py
│   ├── function2.py
│   └─── __init__.py
│
└── two
    ├── another_function.py
    ├── yet_another_function.py
    └─── __init__.py

每个.py(除了__init__.py都是空的)都具有以下内容:

Each .py (except the __init__.py's which are empty) has content along the lines of:

def foo(x):
    return x

def bar(x):
    return x + 2

要使用该模块,请通过以下方式将其导入:import modulename.one.function1.foo.我想做的是在倒数第二个位置找到所有.py文件名,例如function1another_function.

To use the module you import it it in the following way: import modulename.one.function1.foo. What I want to do is find all .py file names in the second to last place, for example function1 or another_function.

到目前为止,建议的解决方案未成功:

The suggested solutions so far has unsuccessfully been:

  • dir(modulename.one)会导致['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__'].

help(modulename.one)实际上包括功能文件名称,标题为PACKAGE CONTENTS.如何获取PACKAGE CONTENTS的列表?

help(modulename.one) which actually includes the function files' names under the title PACKAGE CONTENTS. How do I get a list of the PACKAGE CONTENTS?

我可以(如有人建议的那样)在__init__.py中使用__all__,但是我更喜欢一个简单的内置函数或模块.

I could (as someone suggested) use __all__ in the __init__.py's, but I would prefer a simple built-in function or module.

推荐答案

我认为您正在寻找的是包__init__文件中的__all__定义.

I think what you are looking for is the __all__ definition in your packages __init__files.

您可以看一下: http://mikegrouchy. com/blog/2012/05/be-pythonic -__ init__py.html

解释了软件包中__all__的行为.

which explains the __all__ behavior in packages.

这篇关于获取模块的所有功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆