简体   繁体   English

python更改超类init而不更改子类

[英]python change superclass init without changing subclass

I have a number of servers on multiple devices subclassing from SimpleXMLRPCServer and all tending to run on port 9999. However, I want to interject the interface on which to listen say 我在从SimpleXMLRPCServer子类化的多个设备上有许多服务器,并且所有服务器都倾向于在端口9999上运行。但是,我想插入要侦听的接口

want: 想:

server 0.0.0.0  9999

or: 要么:

server host=0.0.0.0 port=9999

got: 得到:

server (*sys.argv[:1])

and then the server picks the wrong interface on which to listen because of ordering, or dual-homing because the base-class is 'too smart'. 然后服务器由于排序而选择了错误的监听接口,或者由于基类“太聪明”而选择了双归。

The base class produces an IP by selecting one from those available on a local host. 基类通过从本地主机上可用的IP地址中选择一个来生成IP。 How can I specify the IP and not break backward compatility for all the subclasses? 如何指定IP而不破坏所有子类的向后兼容性?

typically each server will accept a port, always seems to be 9999. And then hunt through interfacelist to pick one that is no 127.0.0.1. 通常,每个服务器都将接受一个端口,看起来总是9999。然后在interfacelist中搜寻以选择一个不是127.0.0.1的端口。

Suggestion: if an arg is a number assume its a port. 建议:如果arg是数字,则假定其为端口。 If its an IP assume its a host. 如果它是IP,则假定它是主机。

class Server(SimpleXMLRPCServer):
    def __init__(self, port=1234):
        host = get_ip()
        SimpleXMLRPCServer.__init__(self,addr=(host, port)) 

This feels like a strange compromise, because it reverses the args of the subclasses of SimpleXMLRPCServer: 这感觉像是一个奇怪的折衷,因为它会反转SimpleXMLRPCServer子类的args:

class Server(SimpleXMLRPCServer):
    def __init__(self, port, host=None):
        if host is None:
            host = get_ip()
        SimpleXMLRPCServer.__init__(self,addr=(host, port)) 

if the change affects all the 4 subclasses I would be disappointed, the customers wont know there was a change, but dual homing will now perhaps work with the class being started with an extra argument instead of potluck. 如果更改影响到所有4个子类,我会感到失望,客户将不会知道有更改,但是双重归位现在也许可以在类开始时使用额外的参数而不是杂烩。

the subclasses all do this: 子类都这样做:

class Server(SimpleXMPLRPCServer):
    def __init__(self, port=1234):
       host = get_ip()
       SimpleXMLRPCServer.__init__(self,(host, port)) 

and the main() invocation is: Server(*sys,argv[:1]) 并且main()调用是:Server(* sys,argv [:1])

I'm not sure I understand what you're trying to do, but let me take a stab at this. 我不确定我是否了解您要做什么,但是请允许我a一下。

First, SimpleXMLRPCServer 's constructor takes a (host, port) pair as its addr parameter. 首先, SimpleXMLRPCServer的构造函数将一个(host, port)对作为其addr参数。 So, your subclasses are going to have to look like this: 因此,您的子类将必须如下所示:

class Server(SimpleXMLRPCServer):
    def __init__(self, *whatever):
        # host, port = something
        SimpleXMLRPCServer.__init__(self, (host, port)) 

There's no way around that. 没有办法解决。

If you're just looking to avoid writing the if host is None bit on each subclass, just interpose an intermediate class: 如果您只是想避免在每个子类上编写if host is None位,则只需插入一个中间类即可:

class BaseServer(SimpleXMLRPCServer):
    def __init__(self, port=1234, host=None):
        if host is None:
            host = get_ip()
        SimpleXMLRPCServer.__init__(self, (host, port))

And now, each of your subclasses can be written the way (I think) you want: 现在,您可以按照您想要的方式编写每个子类:

class FirstServer(BaseServer):
    def __init__(self, port=1234, host=None):
        # any other initialization you need to do
        BaseServer.__init__(self, port, host)

class SecondServer(BaseServer):
    # no extra initialization at all needed for this subclass

Of course you could do that by monkeypatching SimpleXMLRPCServer.__init__ to change its interface… by why? 当然,您可以通过猴子修补SimpleXMLRPCServer.__init__来更改其接口来实现此目的……为什么? That's just going to lead to confusion, as anyone reading your code (including you, 6 months from now) is going to have to figure out why SimpleXMLRPCServer isn't acting the way the docs say it should be. 这只会导致混乱,因为任何阅读您的代码的人(包括您,从现在起6个月)都必须弄清楚为什么SimpleXMLRPCServer不能像文档所说的那样起作用。 You get the same benefits out of the intermediate class, without any of the problems. 您从中产阶级中获得了相同的收益,而没有任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM