[Twisted-Python] python properties in twisted don't work

Gabriel Rossetti gabriel.rossetti at arimaz.com
Thu Dec 18 08:35:00 EST 2008


Hello everyone!

I have a problem, if I try to use python property in a twisted program, 
it doesn't really work...the accessor works but as soon as I use the 
mutator, it no longer uses the property (and doesn't set the "real" 
variable. I tried an example without twisted, it works, and with the 
twisted example it doesn't... does anyone know what is going on? Thank you!

Gabriel

#-------------------------------------------------------------------------------------------------------
# This doesn't work, why????
#-------------------------------------------------------------------------------------------------------

from twisted.internet.protocol import Protocol, ClientFactory
from sys import stdout

class Echo(Protocol):

    def __init__(self):
        self.__x = None

    def __getx(self):
        return self.__x
   
    def __setx(self, value):
        if(value != 0):
            self.__x = value
           
    def __delx(self):
        del self.__x

    x = property(__getx, __setx, __delx, "the doc")

    def connectionMade(self):
        t = self.x
        assert t is None
        self.x = 4
        assert self.__x == 4
        t = self.x
        assert t == 4
        self.x = 0
        assert self.__x == 4
        t = self.x
        assert t == 4
       
    def dataReceived(self, data):
        stdout.write(data)


class EchoClientFactory(ClientFactory):
    def startedConnecting(self, connector):
        print 'Started to connect.'

    def buildProtocol(self, addr):
        print 'Connected.'
        return Echo()

    def clientConnectionLost(self, connector, reason):
        print 'Lost connection.  Reason:', reason

    def clientConnectionFailed(self, connector, reason):
        print 'Connection failed. Reason:', reason

if(__name__ == "__main__"):
   
    from twisted.internet import reactor
    reactor.connectTCP("localhost", 4444, EchoClientFactory())
    reactor.run()


#-------------------------------------------------------------------------------------------------------
# This works, as expected
#----------------------------------------------------------------------------------------------------
class C(object):
       
    def __init__(self):
        self._x = None
       
    def __getx(self):
        return self._x
       
    def __setx(self, value):
        if(value != 0):
            self._x = value
           
    def __delx(self):
        del self._x
       
    x = property(__getx, __setx, __delx, "The doc")


if(__name__ == "__main__"):
   
    c = C()
    t = c.x
    assert t is None
    c.x = 4
    assert c._x == 4
    t = c.x
    assert t == 4
    c.x = 0
    assert c._x == 4
    t = c.x
    assert t == 4






More information about the Twisted-Python mailing list