I have a custom Sympy cSymbol
class for the purpose of adding properties to declared symbols. This is done as follows:
class cSymbol(sy.Symbol):
def __init__(self,name,x,**assumptions):
self.x = x
sy.Symbol.__init__(name,**assumptions)
The thing is that when I declare a cSymbol
within a function (say, it affects the property x
of a cSymbol
declared outside the function if the names are the same (here "a"):
def some_function():
dummy = cSymbol("a",x=2)
a = cSymbol("a",x=1)
print(a.x) # >> 1
some_function()
print(a.x) # >> 2, but should be 1
Is there a way to prevent this (other than passing distinct names) ? Actually I am not sure to understand why it behaves like this, I thougt that everything declared within the function would stay local to this function.
Full code below:
import sympy as sy
class cSymbol(sy.Symbol):
def __init__(self,name,x,**assumptions):
self.x = x
sy.Symbol.__init__(name,**assumptions)
def some_function():
a = cSymbol("a",x=2)
if __name__ == "__main__":
a = cSymbol("a",x=1)
print(a.x) # >> 1
some_function()
print(a.x) # >> 2, but should be 1