Python super class with 2 inheritances calling function
Python super class with 2 inheritances calling function
我正在尝试用它的 websocket 包装客户端 rest api,如下所示:
class Client(RequestClient, SubscriptionClient):
def __init__(self, api_key, secret_key):
super().__init__(api_key=api_key, secret_key=secret_key)
def subscribe_event(self, callback, error_handler=None):
super().subscribe_event(callback, error_handler)
def callback(msg):
print(msg)
def error(e):
print(e.error_code + e.error_message)
所以当我直接调用 SubsciptionClient 并执行以下操作时:
client = SubscriptionClient(api_key=api_key, secret_key=secret_key)
client.subscribe_event(callback, error)
我没问题。
但是当我这样做的时候
client = Client(api_key=api_key, secret_key=secret_key)
client.subscribe_event(callback, error)
我收到错误:
AttributeError: 'Client' object has no attribute
'websocket_request_impl'
引擎盖下(删除 init 函数...)
class SubscriptionClient():
def __init__(self, **kwargs):
api_key = None
secret_key = None
if "api_key" in kwargs:
api_key = kwargs["api_key"]
if "secret_key" in kwargs:
secret_key = kwargs["secret_key"]
self.websocket_request_impl = WebsocketRequest(api_key)
def subscribe_event(callback, error):
request = self.websocket_request_impl.subscribe_event(callback, error_handler)
self.__create_connection(request)
class RequestClient(object):
def __init__(self, **kwargs):
api_key = None
secret_key = None
if "api_key" in kwargs:
api_key = kwargs["api_key"]
if "secret_key" in kwargs:
secret_key = kwargs["secret_key"]
知道我在初始化超级 class 时做错了什么吗?
从错误输出中可以看出,RequestClient
的 __init__()
方法没有被调用。 SubscriptionClient
和 RequestClient
都是 object
的子class。您可以使用以下方法验证这一点:
print(issubclass(RequestClient, object))
print(issubclass(SubscriptionClient, object))
# True
# True
你正在处理所谓的cooperative multiple-inheritance
。您创建的结构可视化为:
object
/ \
/ \
RequestClient SubscriptionClient
\ /
\ /
Client
要解决这个问题,需要在每个子class.
的__init__()
方法里面调用父class的__init__()
方法
class SubscriptionClient:
def __init__(self, **kwargs):
super().__init__()
class RequestClient:
def __init__(self, **kwargs):
super().__init__(**kwargs)
class Client(RequestClient, SubscriptionClient):
def __init__(self, api_key, secret_key):
super().__init__(api_key=api_key, secret_key=secret_key)
因为在 Client
声明中 RequestClient
列在 SubscriptionClient
之前,关键字参数将首先传递给 RequestClient
而不是 SubscriptionClient
。
class Client(RequestClient, SubscriptionClient):
我正在尝试用它的 websocket 包装客户端 rest api,如下所示:
class Client(RequestClient, SubscriptionClient):
def __init__(self, api_key, secret_key):
super().__init__(api_key=api_key, secret_key=secret_key)
def subscribe_event(self, callback, error_handler=None):
super().subscribe_event(callback, error_handler)
def callback(msg):
print(msg)
def error(e):
print(e.error_code + e.error_message)
所以当我直接调用 SubsciptionClient 并执行以下操作时:
client = SubscriptionClient(api_key=api_key, secret_key=secret_key)
client.subscribe_event(callback, error)
我没问题。
但是当我这样做的时候
client = Client(api_key=api_key, secret_key=secret_key)
client.subscribe_event(callback, error)
我收到错误:
AttributeError: 'Client' object has no attribute 'websocket_request_impl'
引擎盖下(删除 init 函数...)
class SubscriptionClient():
def __init__(self, **kwargs):
api_key = None
secret_key = None
if "api_key" in kwargs:
api_key = kwargs["api_key"]
if "secret_key" in kwargs:
secret_key = kwargs["secret_key"]
self.websocket_request_impl = WebsocketRequest(api_key)
def subscribe_event(callback, error):
request = self.websocket_request_impl.subscribe_event(callback, error_handler)
self.__create_connection(request)
class RequestClient(object):
def __init__(self, **kwargs):
api_key = None
secret_key = None
if "api_key" in kwargs:
api_key = kwargs["api_key"]
if "secret_key" in kwargs:
secret_key = kwargs["secret_key"]
知道我在初始化超级 class 时做错了什么吗?
从错误输出中可以看出,RequestClient
的 __init__()
方法没有被调用。 SubscriptionClient
和 RequestClient
都是 object
的子class。您可以使用以下方法验证这一点:
print(issubclass(RequestClient, object))
print(issubclass(SubscriptionClient, object))
# True
# True
你正在处理所谓的cooperative multiple-inheritance
。您创建的结构可视化为:
object
/ \
/ \
RequestClient SubscriptionClient
\ /
\ /
Client
要解决这个问题,需要在每个子class.
的__init__()
方法里面调用父class的__init__()
方法
class SubscriptionClient:
def __init__(self, **kwargs):
super().__init__()
class RequestClient:
def __init__(self, **kwargs):
super().__init__(**kwargs)
class Client(RequestClient, SubscriptionClient):
def __init__(self, api_key, secret_key):
super().__init__(api_key=api_key, secret_key=secret_key)
因为在 Client
声明中 RequestClient
列在 SubscriptionClient
之前,关键字参数将首先传递给 RequestClient
而不是 SubscriptionClient
。
class Client(RequestClient, SubscriptionClient):