[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Parent instance attribute access

Gabriel Rossetti

2/25/2008 10:36:00 AM

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

2 Answers

marek.rocki

2/25/2008 11:45:00 AM

0

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)

Gabriel Rossetti

2/25/2008 1:31:00 PM

0


marek.rocki@wp.pl 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
(http://twistedmatrix.com/trac/browser/trunk/twisted/words/xish/xm...)

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