Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #12058

Re: Adding modified methods from another class without subclassing

Date 2011-08-23 10:55 +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> <20110822230850.855c6f2ae90cdc57b05f7a74@johnohagan.com>
Newsgroups comp.lang.python
Message-ID <mailman.335.1314060935.27778.python-list@python.org> (permalink)

Show all headers | View raw


On Mon, 22 Aug 2011 23:08:50 +1000
John O'Hagan <research@johnohagan.com> wrote:

> On Mon, 22 Aug 2011 15:27:36 +1000
> Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote:
[..]
> > Looks like a call for (semi-)automatic delegation!
> > 
> > Try something like this:
> > 
> > 
> > # 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
> 
[..]
> 
> However, I don't understand what the super call is doing. If the method isn't delegated, shouldn't it just fall back to getattr(self, name)?

On reading the __getattr__ docs properly, I see that AttributeError is what should generally happen.
 
> Thanks and regards,
> 
> John
> 

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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