Ask Your Question

Comparing objects in Python

asked 2012-10-28 04:22:44 +0200

SLOtoSF gravatar image

updated 2012-10-28 10:41:04 +0200

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 10:38:29 +0200

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 07:43:49 +0200

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-28 04:22:44 +0200

Seen: 552 times

Last updated: Oct 30 '12