[Python-3000] PEP 3100 Comments
Greg Ewing
greg.ewing at canterbury.ac.nz
Tue May 9 09:15:11 CEST 2006
Talin wrote:
> (Think of a music scoring program, which
> displays various kinds of Midi events in a piano roll view, a drum
> machine view, a text view, or a music notation view.)
There is another way of handling things like this, using
a technique known as "double dispatching". For example
the PianoRollView class would send each of the midi
event objects a display_on_piano_roll request, and the
DrumMachineView class would send display_on_drum_score,
etc.
This technique does tend to be difficult to extend,
however, since all the midi objects need to know about
all the possible display options.
> perhaps what is needed is a way to test only those capabilities. For
> example, instead of "isSequence" we could have "isIndexable",
> "isIterable", and so on.
You can do something like that already using
hasattr(x, '__getitem__') etc. Which is all that
these functions could really do, anyway. So it
would just be a matter of whether it's worth
having a nicer way of spelling them.
> Another possibility is to have a special, empty base class that
> signals "this object is a sequence".
Or you could just have an is_sequence class attribute
that is set on classes wanting to proclaim themselves
to be sequences. I do this sort of thing a lot in the
Pyrex compiler, and it seems to work well there. Of
course I'm the person both defining and using the
interfaces -- it might not be so good in the wider
world.
--
Greg
More information about the Python-3000
mailing list