I am sure that you really do not need this, and there are other ways to solve your problem (not subclasses, but the real problem).
If you really need to make fun of the object, go to the proxy object:
from socket import socket
class PolySocket(object):
def __init__(self, *p):
print "PolySocket init"
self._sock = socket(*p)
def __getattr__(self, name):
return getattr(self._sock, name)
def sendall(self, *p):
print "PolySocket sendall"
return self._sock.sendall(*p)
def send(self, *p):
print "PolySocket send"
return self._sock.send(*p)
def connect(self, *p):
print "connecting..."
self._sock.connect(*p)
print "connected"
HOST = "stackoverflow.com"
PORT = 80
readbuffer = ""
s = PolySocket()
s.connect((HOST, PORT))
s.send("a")
s.sendall("a")
Here's the conclusion:
% python foo.py
PolySocket init
connecting...
connected
PolySocket send
PolySocket sendall
source
share