Simply put, which of the two would be considered cleaner and
more pythonic?
Looking at a python projects code and repository layout, we see the
following directory structure.
/project/core
/project/components/module1
...
/project/components/moduleN
/projects/util
Some modules import other modules, and I see (at the very least) two
(kind of three?) different ways of doing so.
`from project.components.module1 import function1, function2 as func,
CONST1, CONST2 as CONST`
or maybe even (which has as an advantage that it becomes clear which namespace something belongs to
`from project.components.module1 import function1, function2 as module1_function2, CONST1, CONST2 as MODULE1_CONST2`
but then it really just becomes personal preference, as the number of characters saved on typing is almost negative (due to having a more
complex import).
but also the dot notation being used
`from project.components import module1`
where obviously the functions are invoked as `module1.function1` etc
Looking at a python projects code and repository layout, we see the
following directory structure.
/project/core
/project/components/module1
...
/project/components/moduleN
/projects/util
(this is far from complete, but enough to help paint a picture.
Some modules import other modules, and I see (at the very least) two
(kind of three?) different ways of doing so.
`from project.components.module1 import function1, function2 as func,
CONST1, CONST2 as CONST`
or maybe even (which has as an advantage that it becomes clear which >namespace something belongs to
`from project.components.module1 import function1, function2 as >module1_function2, CONST1, CONST2 as MODULE1_CONST2`
but then it really just becomes personal preference, as the number of >characters saved on typing is almost negative (due to having a more
complex import).
but also the dot notation being used
`from project.components import module1`
where obviously the functions are invoked as `module1.function1` etc
I hope the above is clear enough from an example.
Simply put, which of the two would be considered cleaner and more pythonic?
Now for a bit more thought, looking at PEP8, we notes about imports,
but sadly PEP8 does not state which method is better/preferred. While >obviously in the end, it's always the maintainers choice in what they
prefer, so are tabs vs spaces and PEP8 is opinionated about that too
(in a good way, even though I'm not a fan :p).
Side note: Using "...import identifier, ..." does not save storage-space
over "import module" (the whole module is imported regardless, IIRC),
however it does form an "interface" and thus recommend leaning into the "Interface Segregation Principle", or as our InfoSec brethren would say
'the principle of least privilege'. Accordingly, prefer "from ... import
... as ...".
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (2 / 14) |
Uptime: | 49:52:15 |
Calls: | 10,397 |
Calls today: | 5 |
Files: | 14,067 |
Messages: | 6,417,314 |
Posted today: | 1 |