On Wed, 27 Jul 2005 12:44:12 +0530, km <[EMAIL PROTECTED]> wrote:

>Hi all,
>
>In the following code why am i not able to access class A's object attribute - 
>'a' ? I wishto extent  class D with all the attributes of its base classes. 
>how do i do that ?
>
>thanks in advance for enlightment ... 
>
>here's the snippet 
>
>#!/usr/bin/python
>
>class A(object):
>    def __init__(self):
>        self.a = 1
>        
>class B(object):
>    def __init__(self):
>        self.b = 2
>
>class C(object):
>    def __init__(self):
>       self.c = 3
>       
>class D(B, A, C):
>    def __init__(self):
>        self.d = 4
>        super(D, self).__init__()
>        
>if __name__ == '__main__':
>    x =  D()
>    print x.a # errs with - AttributeError
>

super(D, self) is going to find __init__ in the first base in the mro where it's
defined. So x.b will be defined, but not x.a.

I don't know what you are defining, but you could call the __init__ methods
of all the base classes by something like (untested)

    for base in D.mro()[1:]:
        if '__init__' in vars(base): base.__init__(self)

replacing your super line above in class D, but I would be leery of
using __init__ methods that way unless I had a really good rationale.

Regards,
Bengt Richter
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to