Upgrading an instance to a subclass

Terry Reedy tjreedy at udel.edu
Wed Oct 8 20:18:34 CEST 2008


Antoon Pardon wrote:
> I have a subclass of socket.
> 
> class Mysocket (socket):
>    ...
> 
> But when I use the python library it will of course
> just return an instance of socket, like the SocketServer
> module.
> 
> So now I was wondering if it is somehow possible to
> turn this instance into a Mysocket instance, either
> by somehow changing the original instance

Instances of Python-coded classes can usually have __class__ changed. 
But not for builtin, C-coded classes, which I assume

 > or producing
> a new instance that represents the same connection.

Given that socket appears to be immutable, I believe Mysocket would need 
a __new__ method that called socket.__new__.  But I have never worked 
with __new__ functions.




More information about the Python-list mailing list