Reputation: 1057
I have a basic understanding of python, but somewhere I have read that when we import a module using following syntax, it doesn't import attributes defined in specified module which starts with _ (single underscore). Can anybody tell me how it is happening and why it is like that ?
from module.submodule import *
Upvotes: 3
Views: 481
Reputation: 69052
It's by design. Variables starting with an underscore are regarded as for internal use only (not the same as private in other languages). They can still be accessed on the module directly, but they arn't imported on a *
import.
From the documentation about *
imports:
This imports all names except those beginning with an underscore (_). In most cases Python programmers do not use this facility since it introduces an unknown set of names into the interpreter, possibly hiding some things you have already defined.
This is also to tell you that it's discouraged to use a * import, better to explicitly import the things you need. The exception are modules that are designed to be used via * import, that means they have an __all__
attribute (a list containing containing the names of everything the module wants to export).
Upvotes: 7