在 SO 上找到了一些问题,但仍然没有答案...有一个数据类
class Proxy(object):
def __init__(self, ip, port):
self.ip = ip
self.port = port
有一个 getter 类应该从不同的来源读取这些数据
class FileProxyGetter(ProxyGetter):
def __init__(self, fname = "d:\\proxies.txt"):
self.fileName = fname
def Get(self):
proxies = []
f = open(self.fileName)
for l in f.xreadlines():
proxies.append(Proxy.fromstring(l[:-1]))
f.close()
return proxies
def Update(self):
return []
我需要一个具有更多选项的代理类,例如
class SecureProxy(Proxy):
def __init__(self, ip, port):
super(SecureProxy, self).__init__(ip, port)
self.transparent = None
现在我想改进 FileProxyGetter 如下:
class FileSecureProxyGetter(FileProxyGetter):
def Get(self):
proxies = super(FileProxyGetter, self).Get()
secureProxies = []
for proxy in proxies:
# Create or Cast Proxy to SecureProxy.
# The transparent should be initialized to None or any other value that I may need
secureProxies.append(SecureProxy(proxy))
return secureProxies
那么如何在 Python 中普遍地从基类转换或创建派生类的实例。如果不需要更改类会更好。
或者你能建议更多的pythonic方式来开发这种关系和架构吗?