On 29/06/2020 17:16, Oscar Benjamin wrote:

In any significant codebase star-import is a bad idea. It makes it
hard to trace the origin of an imported name when looking at the code.
If I'm looking at the code and I see sp.cos then I expect that sp will
be defined or imported somewhere at the top of the file and I can just
go to the first occurrence of it in the file to see where it comes
from. Likewise if you use "from sympy import cos" and I see cos(2) I
expect that cos will be imported at the top and I can search for that.

I feel there are some good reasons to star import SymPy (which is why this subject keeps coming up) these reasons tend to get downplayed. One is the obvious fact that complicated algebraic equations just don't look nice with sp. prefixing functions like cos(). Another is the fact that I would guess many users only intend to use SymPy even though their code is complicated - not really throw away one-off code. Such users are, in effect, using Sympy as an algebra processing system, and are not interested in Python, though they might want to import their own modules that also star imported SymPy.

SymPy's 750000 lines of code is obviously an extreme case.

There also seems to be a certain inconsistency associated with importing SymPy symbols selectively (tested using 1.6):

import sympy
from sympy import integrate
from sympy import sin
x=sympy.symbols('x')

integrate(sin(x),x)

Returns

-cos(x)

I would have expected -sympy.cos(x), and indeed if you subsequently enter cos(x), you get a name error because cos is not defined!


For these reasons, I think it might be worth devising a set of rules that can be followed so as to import SymPy safely as widely as possible. How about:

1)          Only import SymPy  using  * Thus numpy (say) would have to be imported as

import numpy as np and incur the clumsier syntax of np.cos(...)

This is nearly inevitable because you can't end up with two things called cos.

2)         If you are importing other modules that you have written, use exactly the same import commands in all of them.

Struggling as a newcomer with SymPy without importing everything including sympy.abc (and maybe without knowing the Python language) can be *extremely frustrating .*

David





--
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/b0324ab9-fe2c-e813-9c95-04a0c6864a96%40dbailey.co.uk.

Reply via email to