Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #12139
| Date | 2011-08-24 21:09 +1000 |
|---|---|
| From | John O'Hagan <research@johnohagan.com> |
| Subject | Re: Adding modified methods from another class without subclassing |
| References | <mailman.301.1313989495.27778.python-list@python.org> <4e51e8c9$0$29981$c3e8da3$5496439d@news.astraweb.com> <mailman.314.1314018545.27778.python-list@python.org> <4e5355e2$0$29965$c3e8da3$5496439d@news.astraweb.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.385.1314184178.27778.python-list@python.org> (permalink) |
On Tue, 23 Aug 2011 17:25:22 +1000
Steven D'Aprano <steve+comp.lang.python@pearwood.info> 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 <steve+comp.lang.python@pearwood.info> 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_delegate:
> >> return getattr(self.pitches, name)
> >> return super(MySeq, object).__getattr__(self, name)
> >> # will likely raise AttributeError
> >
[...]
>
> > Also, it doesn't immediately suggest to me a way of modifying method calls
> > (maybe __setattr__?).
>
> What do you mean, "modifying method calls"?
That was a rather badly-worded reference to a function in my original post which modified the action of list methods in ways specific to the new class. As I said, I re-read the docs on __getattr__, and now __setattr__, and I get what they do now.
Thanks for your pointers, I'll get back to work.
Regards,
John
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Adding modified methods from another class without subclassing John O'Hagan <research@johnohagan.com> - 2011-08-22 15:04 +1000
Re: Adding modified methods from another class without subclassing Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-22 15:27 +1000
Re: Adding modified methods from another class without subclassing John O'Hagan <research@johnohagan.com> - 2011-08-22 23:08 +1000
Re: Adding modified methods from another class without subclassing Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-23 17:25 +1000
Re: Adding modified methods from another class without subclassing John O'Hagan <research@johnohagan.com> - 2011-08-24 21:09 +1000
Re: Adding modified methods from another class without subclassing John O'Hagan <research@johnohagan.com> - 2011-08-23 10:55 +1000
Re: Adding modified methods from another class without subclassing Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-23 13:26 +1000
csiph-web