More than you ever wanted to know about objects [was: Is everything a refrence or isn't it]
Alex Martelli
aleax at mail.comcast.net
Sat Jan 14 23:56:38 EST 2006
Steve Holden <steve at holdenweb.com> wrote:
...
> > 3. If two objects are equal with "==", does that
> > mean their values are the same?
>
> Almost universally, yes, although if you know enough about how the
> interpreter works "under the hood" you can define the response of
> instances of your own classes to the "==" operator (by defining their
> __eq__ method), and even define a class whose instances aren't equal to
> anything, even to themselves!
Hmmm... now this may be just be, but I'm quite vary of saying that,
since 1 == 1.0 == 1.0+0j, those three objects's values "are the same".
"Are equal", sure. But I intuitively see "being the same" as a stronger
condition than "being equal".
In mathematics, 1 is not "the same" as 1.0 -- there exists a natural
morphism of integers into reals that _maps_ 1 to 1.0, but they're still
NOT "the same" thing. And similarly for the real-vs-complex case.
Python may differ -- try using those "equal but not the same numbers" as
keys into the same dict, and see. One of the few *surprises* I ever got
from Python...!-)
((I don't think this violates the "introduce no complexity that doesn't
help understanding" rule -- I think the 1==1.0 case is important!))
Alex
More information about the Python-list
mailing list