Upgrading an instance to a subclass

A

Antoon Pardon

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 or producing
a new instance that represents the same connection.
 
O

Orestis Markou

I would suggest rather than inheriting from socket, encapsulate over it:

class MySocket(object):
def __init__(self, socket):
self.socket = socket

Then you don't have to worry about patching instances...
 
T

Terry Reedy

Antoon said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top