Weirdness comparing strings
Scott David Daniels
Scott.Daniels at Acm.Org
Tue Sep 30 09:10:42 EDT 2008
Mr.SpOOn wrote:
> Hi,
> I have this piece of code:
>
> class Note():
Unless you _need_ old-style, use new style.
> ...
> def has_the_same_name(self, note):
> return self == note
Define equality (__eq__) if you want to compare for equality.
> def __str__(self):
> return self.note_name + accidentals[self.accidentals]
>
> __repr__ = __str__
If str and repr are to be equal, just define repr.
class Note(object):
def __init__(self, note, accidentals):
self.note_name = note
self.accidentals = accidentals
def has_the_same_name(self, note):
return self == note
def __eq__(self, other):
return isinstance(other, Note) and (
self.note_name == other.note_name and
self.accidentals == other.accidentals)
def __repr__(self):
return self.note_name + accidentals[self.accidentals]
--Scott David Daniels
Scott.Daniels at Acm.Org
More information about the Python-list
mailing list