Re: Adding modified methods from another class without subclassing

2011-08-24 Thread John O'Hagan
On Tue, 23 Aug 2011 17:25:22 +1000 Steven D'Aprano wrote: > On Mon, 22 Aug 2011 11:08 pm John O'Hagan wrote: > > > On Mon, 22 Aug 2011 15:27:36 +1000 > > Steven D'Aprano wrote: > [...] > >> # Untested > >> class MySeq(object): > >> methods_to_delegate = ('__getitem__', '__len__', ...) > >>

Re: Adding modified methods from another class without subclassing

2011-08-24 Thread John O'Hagan
On Mon, 22 Aug 2011 20:38:30 +0200 Peter Otten <__pete...@web.de> wrote: > John O'Hagan wrote: > > > On Mon, 22 Aug 2011 11:32:18 +0200 > > Peter Otten <__pete...@web.de> wrote: > > > >> John O'Hagan wrote: > >> > >> > I have a class like this: > >> > > >> > class MySeq(): > >> > def __ini

Re: Adding modified methods from another class without subclassing

2011-08-23 Thread Steven D'Aprano
On Mon, 22 Aug 2011 11:08 pm John O'Hagan wrote: > On Mon, 22 Aug 2011 15:27:36 +1000 > Steven D'Aprano wrote: [...] >> # Untested >> class MySeq(object): >> methods_to_delegate = ('__getitem__', '__len__', ...) >> pitches = ... # make sure pitches is defined >> def __getattr__(self,

Re: Adding modified methods from another class without subclassing

2011-08-22 Thread Steven D'Aprano
On Tue, 23 Aug 2011 10:55 am John O'Hagan wrote: >> > # Untested >> > class MySeq(object): >> > methods_to_delegate = ('__getitem__', '__len__', ...) >> > pitches = ...  # make sure pitches is defined >> > def __getattr__(self, name): >> > if name in self.__class__.methods_to_d

Re: Adding modified methods from another class without subclassing

2011-08-22 Thread John O'Hagan
On Mon, 22 Aug 2011 23:08:50 +1000 John O'Hagan wrote: > On Mon, 22 Aug 2011 15:27:36 +1000 > Steven D'Aprano wrote: [..] > > Looks like a call for (semi-)automatic delegation! > > > > Try something like this: > > > > > > # Untested > > class MySeq(object): > > methods_to_delegate = ('__g

Re: Adding modified methods from another class without subclassing

2011-08-22 Thread Peter Otten
John O'Hagan wrote: > On Mon, 22 Aug 2011 11:32:18 +0200 > Peter Otten <__pete...@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 fo

Re: Adding modified methods from another class without subclassing

2011-08-22 Thread John O'Hagan
On Mon, 22 Aug 2011 11:32:18 +0200 Peter Otten <__pete...@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.orde

Re: Adding modified methods from another class without subclassing

2011-08-22 Thread John O'Hagan
On Mon, 22 Aug 2011 15:27:36 +1000 Steven D'Aprano wrote: > On Mon, 22 Aug 2011 03:04 pm John O'Hagan wrote: > > > The "pitches" attribute represents the instances and as such I found > > myself adding a lot of methods like: > > > > def __getitem__(self, index): > > return self.pitches[inde

Re: Adding modified methods from another class without subclassing

2011-08-22 Thread Peter Otten
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 attrib

Re: Adding modified methods from another class without subclassing

2011-08-21 Thread Steven D'Aprano
On Mon, 22 Aug 2011 03:04 pm John O'Hagan wrote: > The "pitches" attribute represents the instances and as such I found > myself adding a lot of methods like: > > def __getitem__(self, index): > return self.pitches[index] > > def __len__(self): > return len(self.pitches) Looks like a c

Adding modified methods from another class without subclassing

2011-08-21 Thread John O'Hagan
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(s