为什么 super().__init__() 不起作用?面向对象

Why does super().__init__() don´t work? OOP

我的问题是来自父 class 的 self.client 即使使用 super() 函数也没有加载。相反只有错误出现:

AttributeError: type object 'AuthorizeOrder' has no attribute 'client'.

很遗憾,我在这里找不到任何错误,希望你们中的一些人知道我如何解决这个问题。

非常感谢

class PayPalClient:

    def __init__(self):
        self.client_id = "XYZ"
        self.client_secret = "XYZ"
        self.environment = SandboxEnvironment(client_id=self.client_id, client_secret=self.client_secret)
        self.client = PayPalHttpClient(self.environment)

class AuthorizeOrder(PayPalClient):

    def __init__(self):
        super().__init__()

    @staticmethod
    def build_request_body():
        return {}

    @classmethod
    def authorize_order(self, order_id, debug=False):
        request = OrdersAuthorizeRequest(order_id)
        request.prefer("return=representation")
        request.request_body(self.build_request_body())
        response = self.client.execute(request)
        return response

问题不在于对 super().__init__() 的调用。问题是您正试图从 class 方法访问实例变量。 class 方法的第一个参数通常命名为 cls 并且是 class 本身,而不是 class.

的实例

有关 classmethodstaticmethod 的更多详细信息,请参阅此答案: