在某某身上发现了一些问题,但仍然没有答案。。。有一个数据类
class Proxy(object):
def __init__(self, ip, port):
self.ip = ip
self.port = port
有一个getter类应该从不同的源读取这些数据
^{pr2}$我需要一个有更多选项的代理类
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中从基类转换或创建派生类的实例呢。如果不改变所需的类会更好。在
或者你能给我一个更像Python的方式来发展这种关系和架构吗?在
您可以使用继承:
但在这种情况下,使用组合可能更有用。在
^{pr2}$编辑:python中切换对象类型的一个肮脏的技巧:
两个不同类型的对象共享同一状态的另一个肮脏伎俩:
然而,这些技巧虽然在Python中是可能的,但我不认为它们是pythonic,也不是一个好的OO设计。在
Python 3.x及更高版本中的另一种可能,它删除了“unbound method”而不是使用常规函数:
相关问题 更多 >
编程相关推荐