Need I add `__all__` list infront of the python model?

Viewed 32

I'm writing some util modules in Python3.10 and want that all function or class defined in
leaf node .py files can be import easily. Consider the following file sturctures:

A0
├─ B0
│    ├─ __init__.py
│    └─ b0.py
├─ B1
│    ├─ __init__.py
│    └─ b1.py
└─ __init__.py

Where b0.py is difined as:

# __all__ = ['func_b0']
def func_b0():...
def _func_b0():...
def __func_b0():...

If I write from .b0 import * into A0/B0/__init__.py, then I can use from A0.B0 import func_b0 in external file to use my custom function func_b0 easily, instead of use from A0.B0.b0 import func_b0.

My question is, need I add such as __all__ = ['func_b0'] into A0/B0/b0.py to control A0/B0/__init__.py's import * behavior? If I use private methods, i.e., underline or double underline, I can also control the import * behavior.

In my opinion, using private methods is a better way to control import * behavior, since only one change is required during maintenance. However, the former __all__=[...] method needs to maintain an additional list.

So, is __all__ not designed to be used here? Is there any PEP to discuss this problem (i.e., maybe import alias)?

0 Answers
Related