Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么super().uu init_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;不行?哎呀_Python_Oop_Paypal_Super - Fatal编程技术网

Python 为什么super().uu init_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;不行?哎呀

Python 为什么super().uu init_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;不行?哎呀,python,oop,paypal,super,Python,Oop,Paypal,Super,我的问题是,父类中的self.client甚至没有使用super()函数加载。相反,只有错误出现: AttributeError:类型对象“AuthorizeOrder”没有属性“client” 不幸的是,我在这里找不到任何错误,我希望你们中有人知道我如何解决这个问题 非常感谢 class PayPalClient: def __init__(self): self.client_id = "XYZ" self.client_secr

我的问题是,父类中的self.client甚至没有使用super()函数加载。相反,只有错误出现:

AttributeError:类型对象“AuthorizeOrder”没有属性“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()。问题是您试图从类方法访问实例变量。类方法的第一个参数通常命名为
cls
,是类本身,而不是类的实例


有关
classmethod
staticmethod
的更多详细信息,请参见此答案:

授权顺序
是一种
@classmethod
,尽管需要访问实例属性-
self
引用类
授权顺序
而不是它的实例。另外请注意,如果一个方法只是调用继承的实现,那么您可以完全省略它(正如
\uuuu init\uuu
所做的那样)。