Please note the deliberate mistake of "y = Symbol('x')", which you may have 
missed when interpreting my question.  You have corrected this deliberate 
mistake in your answer.  My code was valid (it ran and gave the results I 
quoted) and highlighted what was, to me, a surprising behaviour.

As to this being the same as "a = 1; b = 1; a == b; a is b" that is true 
for immutables, but not so for e.g. "a = []; b = []" for which vanilla 
Python gives "a == b" => True and "a is b" => False.  

>From this can I infer that the SymPy Symbol class is immutable?  That might 
clear it up in my mind.

On Wednesday, June 3, 2020 at 9:09:19 PM UTC+1, Jason Moore wrote:
>
> Actually, I don't think your code is valid. You create two distinctly 
> different symbols:
>
> In [1]: import sympy as sm                                                 
>                               
>
> In [2]: x = sm.Symbol('x')                                                 
>                               
>
> In [3]: y = sm.Symbol('y')                                                 
>                               
>
> In [4]: x == y                                                             
>                               
> Out[4]: False
>
> In [5]: x is y                                                             
>                               
> Out[5]: False
>
> In [6]: x + y                                                             
>                                
> Out[6]: x + y
>
> Jason
> moorepants.info
> +01 530-601-9791
>
>
> On Wed, Jun 3, 2020 at 1:06 PM Jason Moore <moore...@gmail.com 
> <javascript:>> wrote:
>
>> Yes, this is intentional. It is really no different than this:
>>
>> In [1]: a = 1                                                             
>>                                
>>
>> In [2]: b = 1                                                             
>>                                
>>
>> In [3]: type(a)                                                           
>>                                
>> Out[3]: int
>>
>> In [4]: type(b)                                                           
>>                                
>> Out[4]: int
>>
>> In [5]: a == b                                                           
>>                                 
>> Out[5]: True
>>
>> In [6]: a is b                                                           
>>                                 
>> Out[6]: True
>>
>> Jason
>> moorepants.info
>> +01 530-601-9791
>>
>>
>> On Wed, Jun 3, 2020 at 12:53 PM James Bateman <james....@gmail.com 
>> <javascript:>> 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)
>>>
>>> The behaviour here is very surprising to me.  I would have expected x 
>>> and y to be different Python objects with __repr__ methods which just so 
>>> happen to return the same string.  Instead, x and y are apparently 
>>> different Python names for the same object (x is y).
>>>
>>> Is this intentional? I think I must misunderstand some deep design 
>>> choice in SymPy, and I can't express my confusion well enough to Google 
>>> it.  Please help!
>>>
>>>
>>> -- 
>>> 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 sy...@googlegroups.com <javascript:>.
>>> To view this discussion on the web visit 
>>> https://groups.google.com/d/msgid/sympy/f0084d3b-db98-43cb-becd-020a368aec87%40googlegroups.com
>>>  
>>> <https://groups.google.com/d/msgid/sympy/f0084d3b-db98-43cb-becd-020a368aec87%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>>

-- 
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/ac56a407-d5e9-4bff-83bf-c0d9a0cb8e63%40googlegroups.com.

Reply via email to