Search code examples
pythonooppaypalsuper

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


my problem is self.client from parent class is not loaded even with super() function. Instead only the error comes:

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

Unfortunately I can't find any error here, I hope someone of you knows how I can solve this problem.

Thanks a lot

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

Solution

  • The problem isn't the call to super().__init__(). The problem is that you're trying to access an instance variable from a class method. The first parameter to a class method would traditionally be named cls and is the class itself, not an instance of the class.

    See this answer for more details on classmethod and staticmethod: https://stackoverflow.com/a/12179752/3228591