在帮助()中填充数据

2024-06-16 12:53:32 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我有以下目录结构:

handy/
  - __init__.py
  - utils.py
  - dir1
    - __init__.py
    - script.py

我可以通过将非关键字写入__init__.py文件来填充DATA中的help(),例如:

# __init__.py
hello = "xyz"
other = "z"
variables = 1

现在,当我提供帮助(handy)时,它会显示:

DATA
    hello = 'xyz'
    other = 'z'
    variables = 1

是否有其他方法从顶级__init__.py文件之外填充帮助DATA,或者这是唯一的方法


Tags: 文件方法py目录hellodatainitscript
1条回答
网友
1楼 · 发布于 2024-06-16 12:53:32

我不确定您的想法,但由于handy/__init__.py是一个可执行脚本,您可以执行以下操作:

__init__.py

from .utils import *
hello = "xyz"
other = "z"
variables = 1

utils.py

UTILS_CONSTANT = 42

def func():
    pass

这将导致:

>>> import handy
>>> help(handy)
Help on package handy:

NAME
    handy

PACKAGE CONTENTS
    utils

DATA
    UTILS_CONSTANT = 42
    hello = 'xyz'
    other = 'z'
    variables = 1

FILE
    c:\stack overflow\handy\__init__.py


>>>

help(handy)显示的内容

相关问题 更多 >