Python 为什么super().uu init_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;不行?哎呀
我的问题是,父类中的self.client甚至没有使用super()函数加载。相反,只有错误出现: AttributeError:类型对象“AuthorizeOrder”没有属性“client” 不幸的是,我在这里找不到任何错误,我希望你们中有人知道我如何解决这个问题 非常感谢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
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
所做的那样)。