Ask Your Question

Comparing objects in Python

asked 2012-10-27 22:22:44 -0500

SLOtoSF gravatar image

updated 2012-10-28 04:41:04 -0500

sage: R = ZZ
sage: S = ZZ
sage: R == S

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?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2012-10-28 04:38:29 -0500

In this example R and S are the same Python object. You are probably comparing different instances of the same class. See here for information on how to customize the comparison operations in Python.

Especially this sentence:

Instances of a class normally compare as non-equal unless the class defines the __cmp__() method. Refer to Basic customization) for information on the use of this method to effect object comparisons.

edit flag offensive delete link more

answered 2012-10-30 01:43:49 -0500

Volker Braun gravatar image

Also, 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).

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2012-10-27 22:22:44 -0500

Seen: 333 times

Last updated: Oct 30 '12