Weirdness comparing strings
Almar Klein
almar.klein at gmail.com
Tue Sep 30 06:53:49 EDT 2008
Hi,
Better post complete code. I don't see where self.note_name is
defined, and what are these accidentals?
you write:
def has_the_same_name(self, note):
return self == note
but this does not implicitly convert self to a string. You'll have to
do in explicitly:
use "return str(self) == note" instead.
Hope this helps,
Almar
2008/9/30 Mr. SpOOn <mr.spoon21 at gmail.com>
> Hi,
> I have this piece of code:
>
> class Note():
> ...
> ...
> def has_the_same_name(self, note):
> return self == note
>
> def __str__(self):
> return self.note_name + accidentals[self.accidentals]
>
> __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?
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080930/336e2b72/attachment-0001.html>
More information about the Python-list
mailing list