How to structure a Python module to limit exported symbols? -
How to structure a Python module to limit exported symbols? -
i writing python module purpose export single info structure. believe means module should export single symbol (e.g. foo
), other symbols beingness underscore-prefixed.
generating info construction takes fair amount of code - how should construction module ensure no symbols within code exported without prefix? 2 possible approaches are:
put generation code @ top-level, beingness careful utilize underscores throughout, e.g.:
_bar = ... _i in ...: _bar.append(...) foo = [_bar, ...]
put generation code within function returns info structure. requires function name utilize underscore. example:
def _generate_foo(): bar = ... in ...: bar.append(...) homecoming [bar, ...] foo = _generate_foo()
is either of these approaches considered better? or, there way construction module preferred?
note using underscore prevents name beingness imported from module import *
(as documented). doesn't create name "private" in real way. people can still see in module doing import module
, module._hiddenstuff
.
for case, should instead utilize __all__
. have code whatever want, @ top of module do:
__all__ = ['foo'] # foo beingness 1 thing want export
this has same effect using underscores much improve when want exclude things instead of include them.
python module
Comments
Post a Comment