Adding modified methods from another class without subclassing
Peter Otten
__peter__ at web.de
Mon Aug 22 14:38:30 EDT 2011
John O'Hagan wrote:
> On Mon, 22 Aug 2011 11:32:18 +0200
> Peter Otten <__peter__ at web.de> wrote:
>
>> John O'Hagan wrote:
>>
>> > I have a class like this:
>> >
>> > class MySeq():
>> > def __init__(self, *seq, c=12):
>> > self.__c = c
>> > self.__pc = sorted(set([i % __c for i in seq]))
>> > self.order = ([[self.__pc.index(i % __c), i // __c] for i in
>> > seq])
>> > #other calculated attributes
>> >
>> > @property
>> > def pitches(self):
>> > return [self.__pc[i[0]] + i[1] * self.__c for i in self.order]
>> >
>> > #other methods
>>
>> That makes me dizzy. Are there any maxims in the Zen of Python that this
>> piece doesn't violate?
>
> "Now is better than never"?
>
> I know it looks crazy to take something apart and put it back together, as
> in this *simplified* *example*
emphasis mine ;)
> , but it does have a meaningful use: reducing a
> series of musical notes to a unique irreducible form "__pc", ("prime form"
> in pitch-class set theory), but keeping track of the actual current
> manifestation of that form via the writeable "order" attribute. That's why
> "pitches" must be called with each reference, because it may change,
> unlike "__pc", which can only change if the instance is reinitialised.
>
> I am open to more elegant suggestions, of course. :)
> [...]
Dunno. Maybe you could inherit from collections.(Mutable)Sequence and
somewhat reduce the number of methods you'd have to implement.
Or you introduce a Pitch (Python) class that knows about its pitch class,
and put that into a normal list:
>>> class Pitch(int):
... @property
... def pitch_class(self):
... return self % 12
... def __repr__(self):
... return "Pitch(%s, pitch_class=%s)" % (self,
self.pitch_class)
...
>>> map(Pitch, [1,0,42])
[Pitch(1, pitch_class=1), Pitch(0, pitch_class=0), Pitch(42, pitch_class=6)]
More information about the Python-list
mailing list