Stefan Ram wrote:
ast <n...@none.fr> wrote or quoted:
Why did you renamed itertools as _itertools ?

   Assume I have a module A.py:

import math
def f(): pass

   . Assume I have an additional module B.py:

import A

   . Now, when I'm editing "B.py" in IDLE and type "A.", IIRC
   IDLE will offer me two possible completions: "A.math" and
   "A.f". The "A.math" makes no sense to me.

`import math` imports the `math` module and binds it to `math` in the global namespace of the `A` module. Since it doesn't have a leading underscore, by default it's considered to be a public attribute of the `A` module, and IDLE is offering all the public attributes of the `A` module for completion.

I want it to go
   away. Therefore, I rewrite A.py as:

import math as _math
def f(): pass

   . Now, Idle will only offer the completion "A.f".

   So, I sometimes use this "import math as _math" style. But then,
   it is simpler for me to /always/ use this style; after all: you
   can't know whether someone eventually will import your module!

You can explicitly declare the public interface of a module by defining `__all__`, listing the names which should be considered part of the module's public interface; see:
- https://docs.python.org/3/reference/simple_stmts.html#the-import-statement
- https://peps.python.org/pep-0008/#public-and-internal-interfaces

Although `from A import *` is generally discouraged, if `A` defines `__all__` then only the names listed in `__all__` are bound in the importing module's namespace. Otherwise, all names from `A` which don't have a leading underscore are considered to be public and bound in the importing module.

I don't use IDLE, but it may be that it also uses `__all__` to determine a module's public API. In that case, setting `__all__ = ["f"]` in `A` should prevent it from offering `math` as a completion (nor any other name that's not in the `__all__` list).

--
Mark.
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to