0

Assuming I want to hide all implementation details of a module, and even adding an underscore [1][2] is not enough, are there practical (or pythonistic) reasons against using del?

mymod/__init__.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']

This way dir(mymod) only shows the API and mymod.tick.tock()/since_epoch() are usable. (Well, almost: mymod.tick betrays the fact that it was once called m2.)

This is useful to only show relevant suggestions in the interactive python shell, see python3 -i -c "import mymod; print(dir(mymod))" then >>> mymodule.<TAB>.

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

  • why convention is not enough? what are you trying to prevent your user from? – georgexsh Oct 01 '17 at 17:39
  • Just to hide some implementation details from new users. Explaining 20 or 30 `_impl..` in an autocomplete are a topic for the next year. And via `import mymod.m2` everything is still reachable. – finite graygreen Oct 01 '17 at 18:05

0 Answers0