Dear list,

First a disclaimer, I am a python novice ;) so apologies beforehand for any incorrect terms and use thereof :)

I have a question about the preferred/pythonic way dealing with imports. But let me start by giving a little bit of an example (which lead me to this question).

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

Likewise, if we look at PEP20, there's quite a few 'guidelines' that suggest the dot-notation. To name a few (opinionated) examples (but note I am just using PEP20 as a helper to give some arguments, I know some people feel strongly against pep20 :p):

* Beautiful is better than ugly.
  - Personally, I think the dot notation is more beautiful, but I understand that some people find the short notation more beautiful and less of an eye sore ;)

* Explicit is better than implicit.
  - To me, this is a strong point. Being more explicit is always better, using the dot notation (if though it's not doing the WHOLE path) is a nice balance in being explicit enough. Code readability comes from being more explicit (without being to terse of course).

* Simple is better than complex.
* Flat is better than nested.
  -  I think having more flat and simple imports, and (while more characters to potentially type in the invocation) more readable invocations would be better?

* Readability counts.
  - The dot notation seems to help in readability. For those unfamiliar (or less familiar) with a code base, it becomes incredibly clear where a something comes from (WITHOUT having to rely on IDE's; sure you can hover over something and see that the IDE has hopefully worked out where something came from, but this doesn't work when just reading a page of code, you aren't going to hover over everything and remember all that context).

* In the face of ambiguity, refuse the temptation to guess.
  - I'd argue this is almost self-explanatory, not using dot-notation makes it more ambiguous where something is coming from.

* There should be one-- and preferably only one --obvious way to do it.
  - This is where my confusion/question obviously comes from, as there's multiple ways of doing it, so maybe PEP8 (or a new one) should more strongly favor one?

* Namespaces are one honking great idea -- let's do more of those!
  - Basically, this question/proposal would 'force/nudge' you into writing more with namespaces by using the namespace as prefix.


So what IS the preferred/more pythonic/recommended way to deal with imports?

Why was this not mentioned in PEP8?

Is it worth while to introduce a new PEP or amend PEP8?


For what it's worth, hallucinating chatgpt/google bard both say the same about this topic, that within the python community, it is generally preferred and a good idea to use dot-notation. But how correct are they lol.


Thank you for reading,

Olliver



--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to