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

v9 -- equality hierarchy #1458

Closed
mscuthbert opened this issue Oct 10, 2022 · 1 comment
Closed

v9 -- equality hierarchy #1458

mscuthbert opened this issue Oct 10, 2022 · 1 comment

Comments

@mscuthbert
Copy link
Member

As we've done with Keywords (#1377) make an equality hierarchy for __eq__ within music21. Comes out of discussion at #1454 and #1457.

All Music21Objects should define a list of attributes that must compare to True in order for the two M21 objects to be equal. In addition to being the same class. And then base.Music21Object can define __eq__ something like:

SENTINEL_SELF = object()
SENTINEL_OTHER = object()

def __eq__(self, other):
    if not isinstance(other, type(self)):
        return False  # or NotImplemented?

    for attr in type(self)._getEqualityAttributes():
        if getattr(self, attr, SENTINEL_SELF) != getattr(other, attr, SENTINEL_OTHER):
            return False
    return True

equalityAttributes = ('id', 'groups', 'duration')

@cache
def _getEqualityAttributes(self):
     cls = type(self)
     equalityAttributes = set()
     for klass in [cls, *cls.mro()]:
          equalityAttributes |= set(klass.equalityAttributes)
    return equalityAttributes

this way it cannot be the case that two GeneralNotes would be not equal but two Notes with the same GeneralNote parameters would be equal.

Subclasses can define other things that need to be equal beyond just attributes, but I think this would be clearer.

Should also document that Streams do contains based on is not ==, which leads to things like:

n = note.Note('C#4')
m = note.Note('C#4')
n == m
True
s = stream.Stream([n])
m in s
False
m in list(s)
True

A new major release is a good time to do this.

Intent

[x] I plan on implementing this myself.
[ ] I am willing to pay to have this feature added.
[ ] I am starting a discussion with the hope that community members will volunteer their time to create this. I understand that individuals work on features of interest to them and that this feature may never be implemented.

@mscuthbert
Copy link
Member Author

Fixed in #1466

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

No branches or pull requests

1 participant