通过'del'

时间:2017-10-01 16:44:10

标签: python python-module code-completion

假设我想要隐藏模块的所有实现细节,甚至添加下划线[1] [2]是不够的,是否存在反对使用del的实际(或pythonistic)原因?

mymod / __初始化__。PY

import time
# m2.py with def tock(): ...
from . import m2 as tick

since_epoch = time.time
summer = time.daylight

# def myfn():
#     return time.ctime() # this fails of course

del time
del m2

__all__ = ['since_epoch', 'summer']

这种方式dir(mymod)仅显示API,mymod.tick.tock()/since_epoch()可用。 (嗯,差不多:mymod.tick背叛了曾经被称为m2 的事实。)

这对于仅在交互式python shell中显示相关建议很有用,请参阅python3 -i -c "import mymod; print(dir(mymod))"然后>>> mymodule.<TAB>

1:Hide external modules when importing a module (e.g. regarding code-completion) / 2:Python: 'Private' module in a package

0 个答案:

没有答案