Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hashing for Chord object #83

Closed
rafaelvalle opened this issue Feb 24, 2015 · 5 comments
Closed

hashing for Chord object #83

rafaelvalle opened this issue Feb 24, 2015 · 5 comments

Comments

@rafaelvalle
Copy link

I'm not sure if this was planned, but the ChordSymbol object has different hashes:
import music21
music21.harmony.
dict_har = {}
chord_a = music21.harmony.ChordSymbol('C7')
chord_b = music21.harmony.ChordSymbol('C7')
dict_har[chord_a] = chord_a
dict_har[chord_b]
KeyError Traceback (most recent call last)

@mscuthbert
Copy link
Member

Generally, Music21Objects should not hash to each other and in fact we're removing the simple == comparison from those that have them. The two ChordSymbols may have different placement in a score so they wouldn't hash to be the same. You can override the hash function if you'd like in your own work.

@rafaelvalle
Copy link
Author

I understand your point and hope you understand that devoid of context as in the example above, a ChordSymbol should hash equally.
Thank you.

@mscuthbert
Copy link
Member

If you want to hash equally just hash the string "C7" and use a lookup table to map that to an object. If you set a hash function for a Music21Object so that multiple objects hash equally you will find that there are major problems. They are not equal in any real sense except that they happen to point to the same notes.

@ahankinson
Copy link
Contributor

It's also pretty easy to "monkey patch" a Python object with a custom equality function by overriding the __eq__ method on an object:

In [1]: class Chord(object):
   ...:     pass
   ...:

In [2]: x = Chord()

In [3]: y = Chord()

In [4]: x == y
Out[4]: False

In [5]: def __eq__(self, other):
   ...:     return True
   ...:

In [6]: Chord.__eq__ = __eq__

In [7]: x == y
Out[7]: True

In this case, the two objects will always be equal since my custom equality object always returns True. If you want to just compare pitches in a Chord you can define the parameters for equality and it will work as you expect.

I think Myke's right on this one, though -- just because two chords share the same pitch content doesn't make them the same.

@rafaelvalle
Copy link
Author

Consider computing pitch transition probabilities, or any ingredient your prefer, given chord.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants