Best practices regarding partial import of hand-selected functions into global namespace
Hi all, I have a question regarding best practices for imports when using scipy. I understand that doing things like `from numpy import *` is discouraged and that it is best to keep the global namespace clean. On the other hand, it can also be useful to hand-select some functions from numpy or scipy and import them into the global namespace. Virtually all of the scipy examples use this code style. For instance from https://docs.scipy.org/doc/scipy/reference/sparse.html (combining some things for brevity): ``` from scipy.sparse import csr_matrix from scipy.sparse import lil_matrix from scipy.sparse.linalg import spsolve from numpy.linalg import solve, norm ... ``` etc. Is this considered acceptable practice, then, as long as you aren't shadowing built-ins and the like? I ask because for any reasonable program you will of course never just use three or four functions, but as you keep developing will quickly get to 20-30 or 50 or more. Is that alright then? Are there any guidelines or best practices regarding this usage, particularly if you have a package with several submodules and etc?
You're right that it's when you have a large program with many imports that `from x import y` can make things harder to read. I think this is why there's PEP 8 language about "Absolute imports are recommended". https://peps.python.org/pep-0008/#imports Hope I'm not coming off as pedantic by sending a link to PEP8. It's just that this is more of a general Python style question -- you'd run into this issue even if you weren't using scipy and numpy. So I'm not sure there would be good practices that are specific to `scipy` + `numpy` users. Obviously it can get really verbose when `numpy` and `scipy` have so many modules + functions packed into them. https://realpython.com/absolute-vs-relative-python-imports/#pros-and-cons-of... https://docs.python-guide.org/writing/structure/#modules YMMV but I find myself using absolute imports more and trying to make the code around them more succinct. Some absolutists wish you could only import modules! https://snarky.ca/if-i-were-designing-imort-from-scratch/ Quelle horreur. hth :) David Nicholson, Ph.D. https://nicholdav.info/ https://github.com/NickleDave Prinz lab <http://www.biology.emory.edu/research/Prinz/>, Emory University, Atlanta, GA, USA On Tue, Mar 22, 2022 at 1:23 AM <battaglia01@gmail.com> wrote:
Hi all,
I have a question regarding best practices for imports when using scipy. I understand that doing things like `from numpy import *` is discouraged and that it is best to keep the global namespace clean. On the other hand, it can also be useful to hand-select some functions from numpy or scipy and import them into the global namespace. Virtually all of the scipy examples use this code style. For instance from https://docs.scipy.org/doc/scipy/reference/sparse.html (combining some things for brevity):
``` from scipy.sparse import csr_matrix from scipy.sparse import lil_matrix from scipy.sparse.linalg import spsolve from numpy.linalg import solve, norm ... ```
etc. Is this considered acceptable practice, then, as long as you aren't shadowing built-ins and the like? I ask because for any reasonable program you will of course never just use three or four functions, but as you keep developing will quickly get to 20-30 or 50 or more. Is that alright then?
Are there any guidelines or best practices regarding this usage, particularly if you have a package with several submodules and etc? _______________________________________________ SciPy-User mailing list -- scipy-user@python.org To unsubscribe send an email to scipy-user-leave@python.org https://mail.python.org/mailman3/lists/scipy-user.python.org/ Member address: nicholdav@gmail.com
Thank you David, I don't think referencing PEP8 is pedantic at all and I appreciate the response. As you say, in some sense it is just a regular Python style question and usually the best answer is just to do `import module`. Perhaps it is also best to just do that with scipy as well, or else you end up with this ridiculously long `from scipy.linalg import ___, ___, ___, ___, ___, ___` string at the top of the program that extends all the way to the next computer. I was just confused there for a minute because of the scipy documentation. (I guess I also tend to use numpy and scipy so much that it's kind of in a middle ground between a third party library and part of "core python" for my uses.) Best, Mike
Thank you David, I don't think referencing PEP8 is pedantic at all and I appreciate the response.
👍 As you say, in some sense it is just a regular Python style question and
usually the best answer is just to do `import module`. Perhaps it is also best to just do that with scipy as well, or else you end up with this ridiculously long `from scipy.linalg import ___, ___, ___, ___, ___, ___` string at the top of the program that extends all the way to the next computer.
One thing I somehow only realized recently: this is valid ``` from scipy.linalg import ( ___, ___, ___, ___, ___, ___ ) ``` which can help with readability / line lengths in situations like that.
participants (2)
-
battaglia01@gmail.com -
David Nicholson