I'm working on a project where my code is spread across different files; for instance, in convex.py
, I invoke methods I've written in rings.py
by calling from rings import *
at the top. In rings
I define a constant p
using mpmath
which is used in the various functions of rings
, so it depends on the precision I've hand-written into rings
(e.g. via mpmath.mp.dps = 40
). However, when using convex
I may later on want p
to have higher precision. Is there a way to make rings
take in some input when I import it into convex
? e.g. maybe I want 50 decimal digits of precision once, and 100 the next use, so a perfect solution would be to let rings
take in an input D
and set mpmath.mp.dps = D
there, and in convex
maybe have
D = 50
from rings[D] import *
(and then use D
elsewhere in the file, and where I can also freely change D
(along with whatever else I'd like to change in convex
, without needing to go back into rings
each time)). (The proposed notation above is to sort of view rings
as a function of D
.)
The only concrete workaround I see would be to modify each of my rings
methods to take inputs as all of the precision-dependent constants which I've defined in rings
, redefine them in convex
, and then pass them as arguments; but this seems like a clunky and overall bad solution.
Let me know if I can clarify any of this. Thanks in advance.
EDIT: Sample code
# rings.py
import mpmath as mp
from mpmath import sqrt
mp.mp.dps = 40
p = sqrt(2)
def f(x):
return p*x
# convex.py
import mpmath as mp
from mpmath import sqrt
from rings import *
D = 50 # or 100
mp.mp.dps = D
q = sqrt(3)
print D+f(q)