First page Back Continue Last page Overview Graphics

Custom-made Modules, 2

from a import *

print(a.a, a._a_under, a.__a_dunder)

print(a.a_func(42))

print(a._a_func_under(42))

print(a.__a_func_dunder(42))

Traceback (most recent call last):

File "/home/bernd/main.py", line 3, in <module>

print(a, _a_under, __a_dunder)

NameError: name '_a_under' is not defined

Like before

a.py:

main.py:


Notes:

, two leading underscores signals to Python that you want the variable to be "private" to the module. If you ever do an import * from mymodule, Python will not import names with two leading underscores into your name space. But if you just do a simple import mymodule and then say dir(mymodule) you will see the "private" variables in the list, and if you explicitly refer to mymodule.__DBNAME__ Python won't care, it will just let you refer to it. The double leading underscores are a major clue to users of your module that you don't want them rebinding that name to some value of their own.