ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 30 Oct 2012 07:43:49 +0100Comparing objects in Pythonhttps://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?Sun, 28 Oct 2012 04:22:44 +0100https://ask.sagemath.org/question/9475/comparing-objects-in-python/Answer 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>
https://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 07:43:49 +0100https://ask.sagemath.org/question/9475/comparing-objects-in-python/?answer=14210#post-id-14210Answer 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>
https://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 10:38:29 +0100https://ask.sagemath.org/question/9475/comparing-objects-in-python/?answer=14201#post-id-14201