Parent instance attribute access

Robert Bossy Robert.Bossy at jouy.inra.fr
Mon Feb 25 12:03:21 CET 2008


Gabriel Rossetti wrote:
> Hello,
>
> I have something weird going on, I have the following (simplified) :
>
> class MyFactory(..., ...):
>
>     def __init__(self, *args, **kwargs):
>         self.args = args
>         self.kwargs = kwargs
>         ...
>
> class MyXmlFactory(MyFactory):
>
>     def __init__(self, *args, **kwargs):
>         MyFactory.__init__(self, *args, **kwargs)
>         #self.args = args
>         #self.kwargs = kwargs
>        ...
>
>     def build(self, addr):
>         p = self.toto(*self.args, **self.kwargs)
>
> when build is called I get this :
>
>     exceptions.AttributeError: MyXmlFactory instance has no attribute 'args'
>
> If I uncomment "self.args = args" and "self.kwargs = kwargs" in 
> __init__(...)
> it works. I find this strange, since in OO MyXmlFactory is a MyFactory 
> and thus has
> "self.args" and "self.kargs", and I explicitly called the paret 
> __init__(...) method, so I tried this small example :
>
>     >>> class A(object):
>     ...     def __init__(self, *args, **kargs):
>     ...             self.args = args
>     ...             self.kargs = kargs
>     ...             self.toto = 3
>     ...
>     >>> class B(A):
>     ...       def __init__(self, *args, **kargs):
>     ...               A.__init__(self, *args, **kargs)
>     ...       def getToto(self):
>     ...              print str(self.toto)
>     ...
>     >>> b = B()
>     >>> b.getToto()
>     3
>
> so what I though is correct, so why does it not work with args and 
> kargs? BTW, If I build a MyFactory and call build, it works as expected.
>   
If you add the following lines ot getToto, it works as expected:
    print self.args
    print self.kargs

The bug must lay  somewhere  else in your code.

RB



More information about the Python-list mailing list