When we use from <module/package> import *
, none of the names that start with a _
will be imported unless the module’s/package’s __all__
list explicitly contains them.
Is this not applicable to variables and functions of a class?
From the below programs, it seems it's not applicable to variables and function inside a class.
_bar
and _check_func
both are executed in test_import.py
. However, _test_func()
throws an error for having leading underscore. Am I missing anything here?
test_var.py
class test:
def __init__(self):
self._bar=15
def test_mod(self):
print("this is test mod function")
def _check_func(self):
print("this is _check_func function")
def _test_func():
print("this is test function var 2")
test_import.py
from test_var import *
p1=test()
print(p1._bar)
p1.test_mod()
p1._check_func()
_test_func()
Output:
15
this is test mod function
this is _check_func function
Traceback (most recent call last):
File "test_import.py", line 8, in <module>
_test_func()
NameError: name '_test_func' is not defined