Skip to content

Commit

Permalink
Merge pull request #1316 from cuthbertLab/key-eq
Browse files Browse the repository at this point in the history
Make `Key` instances compare equal regardless of tonic octave
  • Loading branch information
mscuthbert authored May 23, 2022
2 parents 562aede + eb271ac commit dcfc9fd
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions music21/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -997,15 +997,18 @@ def __str__(self):

def __eq__(self, other):
'''
two Keys are equal if their tonics are equal and their modes are equal
Two Keys are equal if their tonics are equal and their modes are equal.
Changed in v8: keys now compare equal regardless of the octave of their tonics:
>>> k = key.Key(pitch.Pitch('C4'))
>>> k2 = key.Key(pitch.Pitch('C5'))
>>> k == k2
True
'''
try:
if self.tonic == other.tonic and self.mode == other.mode:
return True
else:
return False
except AttributeError:
return False
if not isinstance(other, Key):
return NotImplemented
return self.tonic.name == other.tonic.name and self.mode == other.mode

@property
def relative(self) -> Key:
Expand Down

0 comments on commit dcfc9fd

Please sign in to comment.