First page Back Continue Last page Overview Graphics

Custom-made Modules

import a

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))

42 43 44

a_func!

_a_func_under!

__a_func_dunder!

a = 42

_a_under = 43

__a_dunder = 44

def a_func(x):

return "a_func!"

def _a_func_under(x):

return "_a_func_under!"

def __a_func_dunder(x):

return "__a_func_dunder!"

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.