Re: Best practices regarding partial import of hand-selected functions into global namespace

David Nicholson <[email protected]> Tue, 22 Mar 2022 07:21:22 -0400
Newsgroups gmane.comp.python.scientific.user
Message-ID <CAMabFbWUvt=MWUn=PFf4YNd3ZmueDdV3tkdJMRxsPom77EkY7g@mail.gmail.com>
--===============1184298404780552471==
Content-Type: multipart/alternative; boundary="000000000000ae678005daccd0f3"

--000000000000ae678005daccd0f3
Content-Type: text/plain; charset="UTF-8"

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-absolute-imports
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 <[email protected]> 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 -- [email protected]
> To unsubscribe send an email to [email protected]
> https://mail.python.org/mailman3/lists/scipy-user.python.org/
> Member address: [email protected]
>

--000000000000ae678005daccd0f3
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>You&#39;re right that it&#39;s when you have a large =
program with many imports that `from x import y` can make things harder to =
read.</div><div>I think this is why there&#39;s PEP 8 language about &quot;=
Absolute imports are recommended&quot;.<br></div><div><a href=3D"https://pe=
ps.python.org/pep-0008/#imports">https://peps.python.org/pep-0008/#imports<=
/a></div><div>
<div>
<div>Hope I&#39;m not coming off as pedantic by sending a link to PEP8.</di=
v><div>It&#39;s just that this is more of a general Python style question -=
- you&#39;d run into this issue even if you weren&#39;t using scipy and num=
py.</div><div>So I&#39;m not sure there would be good practices that are sp=
ecific to `scipy` + `numpy` users.<br></div><div><br></div>

</div><div>Obviously it can get really verbose when `numpy` and `scipy` hav=
e so many modules + functions packed into them.</div>

</div><div><a href=3D"https://realpython.com/absolute-vs-relative-python-im=
ports/#pros-and-cons-of-absolute-imports">https://realpython.com/absolute-v=
s-relative-python-imports/#pros-and-cons-of-absolute-imports</a></div><div>=
<a href=3D"https://docs.python-guide.org/writing/structure/#modules">https:=
//docs.python-guide.org/writing/structure/#modules</a></div><div>
YMMV but I find myself using absolute imports more and trying to make the c=
ode around them more succinct.<br></div><div>Some absolutists wish you coul=
d only import modules!<br></div><div><a href=3D"https://snarky.ca/if-i-were=
-designing-imort-from-scratch/">https://snarky.ca/if-i-were-designing-imort=
-from-scratch/</a></div><div>Quelle horreur.</div><div><br></div><div>hth :=
)</div><div><br></div><div><div><div><div dir=3D"ltr" class=3D"gmail_signat=
ure" data-smartmail=3D"gmail_signature"><div dir=3D"ltr"><div><div dir=3D"l=
tr"><div><div dir=3D"ltr"><div><div dir=3D"ltr"><div><div dir=3D"ltr"><div>=
<div dir=3D"ltr">David Nicholson, Ph.D.<br></div><div><a href=3D"https://ni=
choldav.info/" target=3D"_blank">https://nicholdav.info/</a><br></div><div>=
<a href=3D"https://github.com/NickleDave" target=3D"_blank">https://github.=
com/NickleDave</a></div><div><span><a href=3D"http://www.biology.emory.edu/=
research/Prinz/" target=3D"_blank">Prinz lab</a>, Emory University, Atlanta=
, GA, USA<br></span></div></div></div></div></div></div></div></div></div><=
/div></div></div></div><br></div></div></div><br><div class=3D"gmail_quote"=
><div dir=3D"ltr" class=3D"gmail_attr">On Tue, Mar 22, 2022 at 1:23 AM &lt;=
<a href=3D"mailto:[email protected]">[email protected]</a>&gt; wrot=
e:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0=
.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi all,<br>
<br>
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 c=
an also be useful to hand-select some functions from numpy or scipy and imp=
ort them into the global namespace. Virtually all of the scipy examples use=
 this code style. For instance from <a href=3D"https://docs.scipy.org/doc/s=
cipy/reference/sparse.html" rel=3D"noreferrer" target=3D"_blank">https://do=
cs.scipy.org/doc/scipy/reference/sparse.html</a> (combining some things for=
 brevity):<br>
<br>
```<br>
from scipy.sparse import csr_matrix<br>
from scipy.sparse import lil_matrix<br>
from scipy.sparse.linalg import spsolve<br>
from numpy.linalg import solve, norm<br>
...<br>
```<br>
<br>
etc. Is this considered acceptable practice, then, as long as you aren&#39;=
t shadowing built-ins and the like? I ask because for any reasonable progra=
m you will of course never just use three or four functions, but as you kee=
p developing will quickly get to 20-30 or 50 or more. Is that alright then?=
<br>
<br>
Are there any guidelines or best practices regarding this usage, particular=
ly if you have a package with several submodules and etc?<br>
_______________________________________________<br>
SciPy-User mailing list -- <a href=3D"mailto:[email protected]" target=
=3D"_blank">[email protected]</a><br>
To unsubscribe send an email to <a href=3D"mailto:[email protected]=
rg" target=3D"_blank">[email protected]</a><br>
<a href=3D"https://mail.python.org/mailman3/lists/scipy-user.python.org/" r=
el=3D"noreferrer" target=3D"_blank">https://mail.python.org/mailman3/lists/=
scipy-user.python.org/</a><br>
Member address: <a href=3D"mailto:[email protected]" target=3D"_blank">ni=
[email protected]</a><br>
</blockquote></div>

--000000000000ae678005daccd0f3--

--===============1184298404780552471==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
SciPy-User mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/scipy-user.python.org/
Member address: [email protected]

--===============1184298404780552471==--