Instance comparison is not necessarily the same as string comparison.
Neither __str__ nor __repr__ are implicitly used at all for comparison.
In fact, by default a pair of instances are not equal unless they are
the same object. To define comparison to mean something, you need to
define __cmp__ or __eq__.
Trivial example of default comparison:
.... pass
....
False
True
See http://docs.python.org/ref/customization.html for more details.
Ken
Mr.SpOOn wrote:
Neither __str__ nor __repr__ are implicitly used at all for comparison.
In fact, by default a pair of instances are not equal unless they are
the same object. To define comparison to mean something, you need to
define __cmp__ or __eq__.
Trivial example of default comparison:
>>class C:
....
>>c = C()
>>d = C()
>>c==d
>>d = C()
>>c==d
>>c==c
See http://docs.python.org/ref/customization.html for more details.
Ken
Mr.SpOOn wrote:
Hi,
I have this piece of code:
>
class Note():
...
...
def has_the_same_na me(self, note):
return self == note
>
def __str__(self):
return self.note_name + accidentals[self.accidental s]
>
__repr__ = __str__
>
if __name__ == '__main__':
n = Note('B')
n2 = Note('B')
print n
print n2
print n.has_the_same_ name(n2)
>
I'd expect to get "True", because their string representation is
actually the same, instead the output is:
>
B
B
False
>
I think I'm missing something stupid. Where am I wrong?
--
>
>
I have this piece of code:
>
class Note():
...
...
def has_the_same_na me(self, note):
return self == note
>
def __str__(self):
return self.note_name + accidentals[self.accidental s]
>
__repr__ = __str__
>
if __name__ == '__main__':
n = Note('B')
n2 = Note('B')
print n
print n2
print n.has_the_same_ name(n2)
>
I'd expect to get "True", because their string representation is
actually the same, instead the output is:
>
B
B
False
>
I think I'm missing something stupid. Where am I wrong?
--
>
>