Default attribute in base class precludes delegation

Gabriel Genellina gagenellina at softlab.com.ar
Thu Dec 18 22:44:28 EST 2003


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.
I need to avoid this, and return Worker instance's value instead of Base's 
default value.
That is, in the last line of the example, I want: d.bar == 'Hello'
How could it be done?

--- cut ---
# This class does the "real" work and I can modify it as needed
class Worker:
     def __init__(self, foo, bar):
         self.foo = foo
         self.bar = bar

# This is just a base class from which I must inherit but I can't modify it
class Base:
     bar = None

# This is a wrapper for Worker and I can modify it.
# I want to get most attributes from Worker class
# but for 'bar' I always get the default from Base,
# its declaration there effectively hides the attribute
# from the delegated Worked instance.
class Derived(Base):
     def __init__(self, worker):
         self.worker = worker

     def __getattr__(self, name):
         return getattr(self.worker, name)

w = Worker(1234, 'Hello')
print 'w.foo', w.foo    # prints 1234
print 'w.bar', w.bar    # prints Hello
d = Derived(w)
print 'd.foo',d.foo     # prints 1234
print 'd.bar',d.bar     # prints None, I want to get Hello

--- cut ---

Gabriel Genellina
Softlab SRL






More information about the Python-list mailing list