Parent instance attribute access

Gabriel Rossetti gabriel.rossetti at mydeskfriend.com
Mon Feb 25 05:36:14 EST 2008


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.

Thanks,
Gabriel

-- 
www.mydeskfriend.com
PSE - C (EPFL)
1015 Ecublens, Switzerland
Tel: +41 21 601 52 76
Mob: +41 76 442 71 62




More information about the Python-list mailing list