I am writing a Python module whose purpose is to export a single data structure. I believe that this means that my module should export one character (for example, foo ), while all other characters have an underscore with a prefix.
Creating a data structure requires enough code โ how should I structure a module to ensure that no characters in this code are exported without a prefix? Two approaches are possible:
Put the generation code at the top level, being careful when using underscores, for example:
_bar = ... for _i in ...: _bar.append(...) foo = [_bar, ...]
Put the generation code in a function that returns a data structure. To do this, only the function name must be used with an underscore. For instance:
def _generate_foo(): bar = ... for i in ...: bar.append(...) return [bar, ...] foo = _generate_foo()
Is any of these approaches considered better? Or is there any other way to structure this module that would be preferable?
source share