Python 中的子类化和内置方法
为了方便起见,我想对 socket
进行子类化以创建 ICMP 套接字:
class ICMPSocket(socket.socket):
def __init__(self):
socket.socket.__init__(
self,
socket.AF_INET,
socket.SOCK_RAW,
socket.getprotobyname("icmp"))
def sendto(self, data, host):
socket.socket.sendto(self, data, (host, 1))
但是,我无法覆盖 socket.sendto
:
>>> s = icmp.ICMPSocket()
>>> s.sendto
<built-in method sendto of _socket.socket object at 0x100587f00>
这是因为 sendto
是一个“内置方法”。根据数据模型参考,这“实际上是内置模型的不同伪装”函数,这次包含一个作为隐式额外参数传递给 C 函数的对象。”
我的问题:子类化时是否可以重写内置方法?
[编辑]第二个问题:如果不,为什么不呢?
For convenience, I wanted to subclass socket
to create an ICMP socket:
class ICMPSocket(socket.socket):
def __init__(self):
socket.socket.__init__(
self,
socket.AF_INET,
socket.SOCK_RAW,
socket.getprotobyname("icmp"))
def sendto(self, data, host):
socket.socket.sendto(self, data, (host, 1))
However, I can't override socket.sendto
:
>>> s = icmp.ICMPSocket()
>>> s.sendto
<built-in method sendto of _socket.socket object at 0x100587f00>
This is because sendto
is a "built-in method". According to the data model reference, this is "really a different disguise of a built-in function, this time containing an object passed to the C function as an implicit extra argument."
My question: is there anyway to override built-in methods when subclassing?
[Edit] Second question: if not, why not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道这不能回答您的问题,但您可以将套接字放入实例变量中。这也是没有人在评论中建议的。
I know this doesn't answer your question, but you could put the socket into an instance variable. This is what Nobody also suggested in the comments.
重新编辑:我的第一个解决方案不起作用,在纠结了一段时间之后,我可以得出结论,在 python 套接字的情况下,你可以说聚合比继承好得多,但在如果您想知道该怎么做
它使用继承检查这段代码:
Re-edit : My first solution wasn't working, and after straggling with this for sometime , i can conclude that in the case of python socket when you can say that aggregation is much better than inheriting but in case you want to know how you can do
it using inheritance check this code: