ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 30 Oct 2012 01:43:49 -0500Comparing objects in Pythonhttp://ask.sagemath.org/question/9475/comparing-objects-in-python/ sage: R = ZZ
sage: S = ZZ
sage: R == S
<False>
No really this comes up as "True", but for my class which I derived from the Ring Class, something like this comes up as False. How can I avoid this?Sat, 27 Oct 2012 22:22:44 -0500http://ask.sagemath.org/question/9475/comparing-objects-in-python/Answer by burcin for <pre><code>sage: R = ZZ
sage: S = ZZ
sage: R == S
<False>
</code></pre>
<p>No really this comes up as "True", but for my class which I derived from the Ring Class, something like this comes up as False. How can I avoid this?</p>
http://ask.sagemath.org/question/9475/comparing-objects-in-python/?answer=14201#post-id-14201In this example `R` and `S` are the same Python object. You are probably comparing different instances of the same class. See [here](http://docs.python.org/2/library/stdtypes.html#comparisons) for [information on how to customize the comparison operations in Python](http://docs.python.org/2/library/stdtypes.html#comparisons).
Especially this sentence:
> Instances of a class normally compare
> as non-equal unless the class defines
> the [__cmp__()](http://docs.python.org/2/reference/datamodel.html#object.__cmp__) method. Refer to [Basic
> customization](http://docs.python.org/2/reference/datamodel.html#customization)) for information on the
> use of this method to effect object
> comparisons.
Sun, 28 Oct 2012 04:38:29 -0500http://ask.sagemath.org/question/9475/comparing-objects-in-python/?answer=14201#post-id-14201Answer by Volker Braun for <pre><code>sage: R = ZZ
sage: S = ZZ
sage: R == S
<False>
</code></pre>
<p>No really this comes up as "True", but for my class which I derived from the Ring Class, something like this comes up as False. How can I avoid this?</p>
http://ask.sagemath.org/question/9475/comparing-objects-in-python/?answer=14210#post-id-14210Also, your custom ring parent class should make sure that is unique. Either derive from `UniqueRepresentation` (for Python classes) or use a factory function that ensures uniqueness (for Cython classes).Tue, 30 Oct 2012 01:43:49 -0500http://ask.sagemath.org/question/9475/comparing-objects-in-python/?answer=14210#post-id-14210