Parent instance attribute access

G

Gabriel Rossetti

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 :
... def __init__(self, *args, **kargs):
... self.args = args
... self.kargs = kargs
... self.toto = 3
... ... def __init__(self, *args, **kargs):
... A.__init__(self, *args, **kargs)
... def getToto(self):
... print str(self.toto)
... 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
 
M

marek.rocki

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)
 
G

Gabriel Rossetti

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
(http://twistedmatrix.com/trac/browser/trunk/twisted/words/xish/xmlstream.py)

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)
and
protocol.ReconnectingClientFactory.

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).

Thanks,
Gabriel


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

class XmlStreamTestSrv(xmlstream.XmlStream):

def __init__(self):
xmlstream.XmlStream.__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):
self.resetDelay()
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())
reactor.run()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,127
Latest member
CyberDefense
Top