[Twisted-Python] python properties in twisted don't work
Drew Smathers
drew.smathers at gmail.com
Thu Dec 18 09:04:44 EST 2008
Protocol is an old-style class - doesn't inherit from object - so
property won't work in that context. This won't be a problem in
python 3 - old-style/new-style classes are consolidated.
-Drew
On Thu, Dec 18, 2008 at 8:35 AM, Gabriel Rossetti
<gabriel.rossetti at arimaz.com> wrote:
> 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
>
>
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>
--
\\\\\/\"/\\\\\\\\\\\
\\\\/ // //\/\\\\\\\
\\\/ \\// /\ \/\\\\
\\/ /\/ / /\/ /\ \\\
\/ / /\/ /\ /\\\ \\
/ /\\\ /\\\ \\\\\/\
\/\\\\\/\\\\\/\\\\\\
d.p.s
More information about the Twisted-Python
mailing list