4 ms·
I love how the imports in https://github.com/alfiopuglisi/guietta/blob/master/guietta/guietta.py https://github.com/alfiopuglisi/guietta/blob/master/guietta/...
by samanator 6y ago
I love how the imports in https://github.com/alfiopuglisi/guietta/blob/master/guietta/guietta.py https://github.com/alfiopuglisi/guietta/blob/master/guietta/... are ordered from shortest to longest. Makes them look like stairs! Very aesthetically pleasing :). Might take that up in my next project.
- teekert 6y agoSomeone once told that that is the way to do it and so I always do (this is not how I normally operate ;)). There is no practical use though I think. But, it looks nice :) Kind of like a phonebook starting with companies like AAAGarden, now you want your module name to be short to get to the top ;) Edit: Found some discussion on this topic: https://stackoverflow.com/questions/20762662/whats-the-correct-way-to-sort-python-import-x-and-from-x-import-y-statement https://stackoverflow.com/questions/20762662/whats-the-corre...
- skocznymroczny 6y agoSome of Python linters enforce such style. If you enable pylint or black I think they do that by default.
- diarrhea 6y agoblack sorts alphabetically, with standard lib first, then 3rd party imports, and user/local imports last. Blocks are separated by a blank line. `from ..` imports come after `import ..`. Especially the alphabetic ordering is important in my view. Makes parsing/reading and spotting duplicates much easier. It is also PEP8 style [0]. For all these reasons, I think the pyramid style is awful in comparison. 0: https://www.python.org/dev/peps/pep-0008/#imports https://www.python.org/dev/peps/pep-0008/#imports
- StavrosK 6y agoBlack doesn't sort imports at all.
- spappal 6y agoAnd PEP 8 doesn't mention sorting the imports. There are even counterexamples which aren't alphabetically sorted. It does mention grouping though. Since Python imports can have side effects, the order can matter. But to the extent that it doesn't break anything, alphabetically sorted groups seems deterministic and readable. I think this might be the most popular implementation for auto-sorting: https://pypi.org/project/isort/ https://pypi.org/project/isort/
- StavrosK 6y agoisort is the most popular, but this is the best by far: https://github.com/asottile/reorder_python_imports https://github.com/asottile/reorder_python_imports