Ask Your Question
0

Comparing objects in Python

asked 12 years ago

SLOtoSF gravatar image

updated 12 years ago

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?

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
1

answered 12 years ago

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.

Preview: (hide)
link
1

answered 12 years ago

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

Preview: (hide)
link

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 12 years ago

Seen: 640 times

Last updated: Oct 30 '12