其他分享
首页 > 其他分享> > 解决:AttributeError: 'GovernmentApiClient' object has no attribute 're_fund'

解决:AttributeError: 'GovernmentApiClient' object has no attribute 're_fund'

作者:互联网

代码如下:

 1 class GovernmentApiClient:
 2 
 3     def refund(self):
 4         print('123456')
 5         '''退款返销'''
 6         body = {
 7             "thirdOrderNo": "5rd20220829173058",
 8             "flowNo": f"refund-{times()}" }
 9         res = sendPostRequest(self.host1,body,self.res_fund).text
10         print(res)
11         return res
12 
13 if __name__ == '__main__':
14    GovernmentApiClient().refund()  #退款返销

 报错如下:

Traceback (most recent call last):
  File "D:/Pycharm_project/government/utils/government_api.py", line 14, in <module>
    GovernmentApiClient().re_fund()  #退款返销
AttributeError: 'GovernmentApiClient' object has no attribute 'refund'

问题分析:

refund函数中,开始执行时,加了print“123456”,然而并没有打印出“123456”,所以我们可以初步判断出,当我们去GovernmentApiClient().refund()时,并没有执行refund()
基于上述分析,把代码调整为:
class GovernmentApiClient:

 def re_fund(self):
  print('123456')
  '''退款返销'''
  body = {
   "thirdOrderNo": "5rd20220830091415",
   "flowNo": f"refund-{times()}"
  }
  res = sendPostRequest(self.host1, body, self.res_fund).text
  print(res)
  return res

if __name__ == '__main__':
 GovernmentApiClient().re_fund()  # 退款返销
结果:
123456
{"code":200,"msg":"处理成功","time":1661822084978,"success":true,"data":{"platformOrderNo":"LO2022083000000004","outOrderNo":"5rd20220830091415","tradeTime":"20220830091444"}}
运行正常了,就可以说明之前GovernmentApiClient().refund()时,并没有执行refund(),运行的是其他的,我们把函数名更改为re_fund,就可以了。

标签:refund,__,no,attribute,self,object,fund,GovernmentApiClient,res
来源: https://www.cnblogs.com/brf-test/p/16636884.html