Default attribute in base class precludes delegation

Andrew Bennetts andrew-pythonlist at puzzling.org
Fri Dec 19 07:42:55 CET 2003


On Fri, Dec 19, 2003 at 12:44:28AM -0300, Gabriel Genellina wrote:
> Hi
> 
> In the following code sample, I have:
> - a Worker class, which could have a lot of methods and attributes. In 
> particular, it has a 'bar' attribute. This class can be modified as needed.
> - a Base class (old-style) which defines a default class attribute 'bar' 
> too. I can't modify the source code of this class. Note that Workes does 
> not inherit from Base.
> - a Derived class which must inherit from Base and is a wrapper around 
> Worker: it contains a Worker instance and delegates almost anything to it. 
> This class can be modified as needed too.
> 
> For most attributes, as they are not found in the Derived instance's dict, 
> __getattr__ is called and the attribute is retrieved from the Worker 
> instance.
> But for the 'bar' attribute, it is found in the Base class and just the 
> default value is returned. __getattr__ is never called.

Inherit Derived from object as well as Base, to make it new-style, then you
could use either __getattribute__ or descriptors (e.g. property) to get the
behaviour you want.

-Andrew.






More information about the Python-list mailing list