Parent instance attribute access

Gabriel Rossetti gabriel.rossetti at
Mon Feb 25 14:30:58 CET 2008

marek.rocki at wrote:
> Hello. Please post the minimal code that can be run and demonstrates
> what's wrong. I tried the following and it works fine, args and kwargs
> are visible:
> class MyFactory(object):
> 	def __init__(self, *args, **kwargs):
> 		self.args = args
> 		self.kwargs = kwargs
> class MyXmlFactory(MyFactory):
> 	def __init__(self, *args, **kwargs):
> 		MyFactory.__init__(self, *args, **kwargs)
> 	def build(self, addr):
> 		p = self.toto(*self.args, **self.kwargs)
> 		return p
> 	def toto(self, *args, **kwargs):
> 		print 'args in toto:', args
> 		print 'kwargs in toto:', kwargs
> 		return 'ok'
> print MyXmlFactory(1, 2, 3, four = 4, five = 5).build(None)
Yes, I see, here is an example, it's using twisted, but I think it's a 
python problem (that's why I wrote here). The code for the parent 
classes is here 

xmlstream.XmlStreamFactory in the following code is supposed to be 
MyFactory in the previous example, and MyXmlFactory is supposed to be 
(xmlstream.XmlStreamFactory. If you uncomment the two commented lines in 
MyXmlStreamFactory it works, if they are commented it doesn't. The 
parent class (XmlStreamFactory) inherits from two classes :

   XmlStreamFactoryMixin (where args and kwargs are defined)

I think it may be because of the multiple inheritance that I'm getting 
this, maybe since the child class doesn't define the __init__() method.

To test, run the following code in one terminal, and run :

    netcat localhost 4321

in another (it will crash right away with the lines commented).


from twisted.words.xish import xmlstream
from twisted.internet import reactor

class XmlStreamTestSrv(xmlstream.XmlStream):

    def __init__(self):

    def _onHeader(self, element):
        print "got header from %s : %s" % 
(str(self.transport.getPeer().host), element.toXml())

    def connected(self, xs):
        print 'Connection from %s!' % str(self.transport.getPeer().host)
        xs.addObserver("/header", self._onHeader)

class MyXmlStreamFactory(xmlstream.XmlStreamFactory):

    def __init__(self, *args, **kwargs):
        xmlstream.XmlStreamFactory.__init__(self, *args, **kwargs)
        #self.args = args
        #self.kwargs = kwargs
        self.protocol = XmlStreamTestSrv

    def buildProtocol(self, addr):
        xs = self.protocol(*self.args, **self.kwargs)
        xs.factory = self
        self.addBootstrap(xmlstream.STREAM_CONNECTED_EVENT, xs.connected)
        for event, fn in self.bootstraps:
            xs.addObserver(event, fn)
        return xs

if __name__ == "__main__":

    reactor.listenTCP(4321, MyXmlStreamFactory())

More information about the Python-list mailing list