Hi,
I am writing some Python functions using SageMath (with the SageMath Kernel 10.7 on Mac, in Jupyter Lab). I have a question about how to safely pass vars into a Python function, if it's possible at all. The intent is to make the function usable no matter what variable the caller chooses for his function. The issue shows up in the substitute function, but of course I'd like the solution to work in other contexts as well. (At least in some cases, it seems to). Is there another way to approach this?
Thanks!
Here's a highly simplified example that shows the issue:
def example_reasonable(fn, variable, value):
"""This is a simple example showing the issue; this example doesn't work"""
# print(variable)
# prints u as expected....
# but this substitution doesn't work
return fn.substitute(variable=value)
def example_works(fn, value):
return fn.substitute(u=value)
u = var('u')
f = u^2 + 7*u
print(example_reasonable(f, u, 3)) # Output should be 30, is just function
print(example_works(f, 3)) # Output should be 30, and is 30
Output:
u^2 + 7*u
30