Classes for pseudo-m21 objects to inherit from. The most import attributes that nearly everything in music21 – not just things that live in streams – should inherit from are given below.

Concept borrowed from m21j.


class music21.prebase.ProtoM21Object

A class for pseudo-m21 objects to inherit from.

Cannot be put into streams.

ProtoM21Object read-only properties


Returns a set (that is, unordered, but indexed) of all of the classes that this class belongs to, including string names, fullyQualified string names, and objects themselves.

It’s cached on a per class basis, so makes for a really fast way of checking to see if something belongs to a particular class when you don’t know if the user has given a string, a fully qualified string name, or an object.

Did I mention it’s fast? It’s a drop in substitute for the deprecated .isClassOrSubclass. It’s not as fast as x in n.classes or isinstance(n, x) if you know whether it’s a string or class, but this is good and safe.

>>> n = note.Note()
>>> 'Note' in n.classSet
>>> 'music21.note.Note' in n.classSet
>>> note.Note in n.classSet
>>> 'Rest' in n.classSet
>>> note.Rest in n.classSet
>>> object in n.classSet
>>> sorted([s for s in n.classSet if isinstance(s, str)])
['GeneralNote', 'Music21Object', 'NotRest', 'Note', 'ProtoM21Object',
 'music21.note.GeneralNote', 'music21.note.NotRest', 'music21.note.Note',
>>> sorted([s for s in n.classSet if not isinstance(s, str)], key=lambda x: x.__name__)
[<class 'music21.note.GeneralNote'>,
 <class 'music21.base.Music21Object'>,
 <class 'music21.note.NotRest'>,
 <class 'music21.note.Note'>,
 <class 'music21.prebase.ProtoM21Object'>,
 <class 'object'>]

Returns a tuple containing the names (strings, not objects) of classes that this object belongs to – starting with the object’s class name and going up the mro() for the object.

Notes are Music21Objects:

>>> n = note.Note('C#')
>>> n.classes
('Note', 'NotRest', 'GeneralNote', 'Music21Object', 'ProtoM21Object', 'object')

Durations are not, but they inherit from ProtoM21Object

>>> d = duration.Duration('half')
>>> d.classes
('Duration', 'ProtoM21Object', 'SlottedObjectMixin', 'object')

Having quick access to these things as strings makes it easier to do comparisons:

Example: find GClefs that are not Treble clefs (or treble 8vb, etc.):

>>> s = stream.Stream()
>>> s.insert(10, clef.GClef())
>>> s.insert(20, clef.TrebleClef())
>>> s.insert(30, clef.FrenchViolinClef())
>>> s.insert(40, clef.Treble8vbClef())
>>> s.insert(50, clef.BassClef())
>>> s2 = stream.Stream()
>>> for t in s:
...    if 'GClef' in t.classes and 'TrebleClef' not in t.classes:
...        s2.insert(t)
{10.0} <music21.clef.GClef>
{30.0} <music21.clef.FrenchViolinClef>

Changed 2015 Sep: returns a tuple, not a list.

ProtoM21Object methods

ProtoM21Object.isClassOrSubclass(classFilterList: Sequence) → bool

Given a class filter list (a list or tuple must be submitted), which may have strings or class objects, determine if this class is of the provided classes or a subclasses.

NOTE: this is a performance critical operation for performance, only accept lists or tuples

>>> n = note.Note()
>>> n.isClassOrSubclass(('Note',))
>>> n.isClassOrSubclass(('GeneralNote',))
>>> n.isClassOrSubclass((note.Note,))
>>> n.isClassOrSubclass((note.Rest,))
>>> n.isClassOrSubclass((note.Note, note.Rest))
>>> n.isClassOrSubclass(('Rest', 'Note'))