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
On Thu, 18 Dec 2008 14:35:00 +0100, Gabriel Rossetti gabriel.rossetti@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!
Property setters only work on new-style classes. Many classes defined by Twisted are classic, so they will not work with properties unless you make your subclass new-style, which you can do by mixing in object.
Jean-Paul
Jean-Paul Calderone wrote:
On Thu, 18 Dec 2008 14:35:00 +0100, Gabriel Rossetti gabriel.rossetti@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!
Property setters only work on new-style classes. Many classes defined by Twisted are classic, so they will not work with properties unless you make your subclass new-style, which you can do by mixing in object.
Jean-Paul
Thank you, I wasn't aware of that, I'll add object when inheriting from any Twisted stuff then.
Gabriel
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@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@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
On Dec 18, 2008, at 6:04 AM, Drew Smathers wrote:
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.
Is there any interest in adding 'object' to some of the core classes like Deferred, in a future release of Twisted? It would help with some debugging/logging (because there's better introspection)
As much as I'm looking forward to using Python 3, I can't imagine I'll be using it for any real projects that I have to deploy for at least 2 years...
Alec
-Drew
On Thu, Dec 18, 2008 at 8:35 AM, Gabriel Rossetti gabriel.rossetti@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@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
-- \\/"/\\\\\\ \\/ // ///\\\\ \/ \// /\ /\\ \/ // / // /\ \\ / / // /\ /\\ \ / /\\ /\\ \\/\ /\\/\\/\\\ d.p.s
Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
* Alec Flett alecf@metaweb.com [2008-12-18 11:03:48 -0800]:
On Dec 18, 2008, at 6:04 AM, Drew Smathers wrote:
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.
Is there any interest in adding 'object' to some of the core classes like Deferred, in a future release of Twisted? It would help with some debugging/logging (because there's better introspection)
The proble with doing this is that it is a backwards-incompatible change; doing this in a way that would comply with Twisted's backwards compatibility policy would be somewhat awkward:
http://twistedmatrix.com/trac/wiki/CompatibilityPolicy
What sort of better introspection are you talking about?
On Dec 18, 2008, at 3:06 PM, Tristan Seligmann wrote:
- Alec Flett alecf@metaweb.com [2008-12-18 11:03:48 -0800]:
On Dec 18, 2008, at 6:04 AM, Drew Smathers wrote:
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.
Is there any interest in adding 'object' to some of the core classes like Deferred, in a future release of Twisted? It would help with some debugging/logging (because there's better introspection)
The proble with doing this is that it is a backwards-incompatible change; doing this in a way that would comply with Twisted's backwards compatibility policy would be somewhat awkward:
The only way this could be done would be to do so in an "opt-in" fashion. E.g. something like: import twisted twisted.useNewStyleClasses(True)
But even that would be global for a given instantiation of the python interpreter which isn't really very good either if you need to mix new- style-expecting and old-style-expecting classes.
So it really doesn't esem worth it, given that people can mixin object themselves if they desire.
James