>> >> On Wed, Jun 3, 2020 at 12:53 PM James Bateman <james....@gmail.com> wrote: >> >>> >> >>> I've just discovered a bug in my code which boiled down to the >> >>> following, where a symbol "y" was given the same SymPy name as an >> >>> existing symbol. >> >>> >> >>> import sympy as sp >> >>> x = sp.Symbol('x') >> >>> y = sp.Symbol('y') >> >>> >> >>> x == y # True >> >>> x is y # True; expected False >> >>> x + y # 2*x; expected x + x (which would have made the bug in my code >> >>> more apparent)
On Wed, 3 Jun 2020 at 23:18, James Bateman <james.bate...@gmail.com> wrote: > > Thank you, but I don't need help debugging my code; I had a typo which boiled > down to the example I gave. My only question was whether this was intended > behaviour. > > Please note the deliberate mistake of "y = Symbol('x')", which you may have > missed when interpreting my question. True is returned for both "x == y" and > "x is y" in SymPy 1.2 (local installation) and SymPy 1.5.1 > (http://live.sympy.org). I think you maybe forgot to make the deliberate mistake when posting here (see above). The name of the Python variable does not need to match the name of the symbol. Sympy has no way of knowing what name you use for the Python variable so if you do x = Symbol('y') then there is no way for sympy to know that you assigned the result to a variable called x. That's not so much an intended feature but just how Python works. Symbols with the same name (and assumptions) as considered equivalent in sympy which is useful in many situations. If you want to create a symbol that will only ever compare equal to itself regardless of the name of any other symbol then you can use Dummy: https://docs.sympy.org/latest/modules/core.html#dummy Dummy is a bit awkward to use in some situations which is why it isn't the default behaviour for symbols in sympy. -- Oscar -- 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 sympy+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAHVvXxS45b-%2B3vw0YuAgWUpDVhrH7AGXNjCYJZpTxoQ_XkENRA%40mail.gmail.com.