Oh yeah. I just remembered that you'll want to override __getattr__ for that to work. And it occurs to me that socket.accept is a method on socket.socket and not a module level function of socket as I treated it in my first post. So the code will be something like:
Code:
class IrcSocket: #No need to extend object because this is p3k
def __init__(self, id, socket):
self.id = id
self._socket = socket
def send(self, bytes):
log(bytes)
socket.socket.sendall(self._socket, bytes)
def accept(self, *args, **kwargs):
tmp_socket = self._socket.accept(*args, **kwargs)
return type(self)(some_id*args, tmp_socket)
def __getattr__(self, attr):
return self._socket.__dict__[attr]
The type operator returns the type of the argument (when it's called with one argument. with three, it creates a new type/class). Here we're using it to get at IrcSocket. Doing it that way avoids hardcoding the class into the code so that derived classes will return derived classes rather than IrcSocket.
__getattr__ will fetch attributes that can't be found on IrcSocket from its _socket attribute.
Bookmarks