I’ve notice this. Example:
create an empty text file called for example ast.py
$ touch ast.py
run Python
$ python
>>> from ast import *
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__']
>>> from _ast import *
>>> dir()
['AST', 'Add', 'And', 'Assert', 'Assign', ...]
ast is a python module. So… what’s happening here? I tried with an empty os.py and didn’t work.
There’s nothing special with
_xxxmodules except they are private (e.g._abcoll) or low-level (e.g._thread) and not intended to be used in general.The
_astmodule is special, e.g.But this is not because of the leading
_, but that_astis a built-in module. Similar thing happens with thesysmodule.In Python
_astandastare two separate modules. There is a linein the built-in
ast.py, so importingastwill also bring everything from the_astmodule in. This gives you the illusion that_astandasthave the same content, but actually_astis a lower level module ofast.