在哪里用tryexcept包装函数更好?

2024-04-26 01:06:49 发布

您现在位置:Python中文网/ 问答频道 /正文

下一个函数将因某些数据而失败。在哪里用试块包装比较好?你知道吗

 if attr in obj_request:
     new_record = GosReestr()
     new_record.display_name_department = request.user.profile_ad.department
     new_record.set_data(**obj_request[attr][_main_data])
     print('before')
     new_record.save()
     print('after')

这样更好吗?你知道吗

 if attr in obj_request:
     new_record = GosReestr()
     new_record.display_name_department = request.user.profile_ad.department
     new_record.set_data(**obj_request[attr][_main_data])
     print('before')
     try:
          new_record.save()
     except as e
     print('after')

还是将所有内容都封装在一个if块中更好?你知道吗


Tags: nameinobjnewdataifrequestdisplay