鱼C论坛

 找回密码
 立即注册
查看: 331|回复: 1

[技术交流] AssertionError-PythonBIF(2)

[复制链接]
发表于 2025-1-27 12:19:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 zyx2012 于 2025-1-26 18:21 编辑

原文:
  1. Help on class AssertionError in module builtins:

  2. class AssertionError(Exception)
  3. |  Assertion failed.
  4. |
  5. |  Method resolution order:
  6. |      AssertionError
  7. |      Exception
  8. |      BaseException
  9. |      object
  10. |
  11. |  Methods defined here:
  12. |
  13. |  __init__(self, /, *args, **kwargs)
  14. |      Initialize self.  See help(type(self)) for accurate signature.
  15. |
  16. |  ----------------------------------------------------------------------
  17. |  Static methods defined here:
  18. |
  19. |  __new__(*args, **kwargs)
  20. |      Create and return a new object.  See help(type) for accurate signature.
  21. |
  22. |  ----------------------------------------------------------------------
  23. |  Methods inherited from BaseException:
  24. |
  25. |  __getattribute__(self, name, /)
  26. |      Return getattr(self, name).
  27. |
  28. |  __reduce__(self, /)
  29. |      Helper for pickle.
  30. |
  31. |  __repr__(self, /)
  32. |      Return repr(self).
  33. |
  34. |  __setstate__(self, object, /)
  35. |
  36. |  __str__(self, /)
  37. |      Return str(self).
  38. |
  39. |  add_note(self, object, /)
  40. |      Exception.add_note(note) --
  41. |      add a note to the exception
  42. |
  43. |  with_traceback(self, object, /)
  44. |      Exception.with_traceback(tb) --
  45. |      set self.__traceback__ to tb and return self.
  46. |
  47. |  ----------------------------------------------------------------------
  48. |  Data descriptors inherited from BaseException:
  49. |
  50. |  __cause__
  51. |      exception cause
  52. |
  53. |  __context__
  54. |      exception context
  55. |
  56. |  __dict__
  57. |
  58. |  __suppress_context__
  59. |
  60. |  __traceback__
  61. |
  62. |  args
复制代码

翻译:

  1. 继承自 BaseException 的方法:
  2. |
  3. | __getattribute__(self, name, /)
  4. | 返回 getattr(self,name)。
  5. |
  6. | __reduce__(self, /)
  7. | pickle 的辅助函数。
  8. |
  9. | 返回 __repr__(self, /)
  10. | 返回 repr(self)。
  11. |
  12. | __setstate__(self, object, /)
  13. |
  14. |__str__(self, /)
  15. | 返回 str(self)。
  16. |
  17. | add_note(self, object, /)
  18. | Exception.add_note(note) --
  19. | 为异常添加注释
  20. |
  21. | with_traceback(self, object, /)
  22. | Exception.with_traceback(tb) -- | 为异常添加注释。
  23. | 将 self.__traceback__ 设为 tb 并返回 self。
  24. |
  25. | ----------------------------------------------------------------------
  26. | 从 BaseException 继承的数据描述符:
  27. |
  28. | __cause__
  29. | 异常原因
  30. |
  31. | __context__
  32. | 异常上下文
  33. |
  34. | __dict__
  35. |
  36. | __suppress_context__
  37. |
  38. | __traceback__
  39. |
  40. | args
复制代码

对于这些异常子类, 很多都是重复的,所以如果重复的没啥意义的我就直接机翻了,不然效率太低,请见谅

用法:

  提到AssertionError,就一定与assert(断言)相联系.
  
  语法:
  
  1. assert expression [, arguments]
复制代码
#assert 表达式[,参数]
  
  assert用于调试(反正我只用来调试),只要assert后的表达式为真,那么将不会发生任何事情,如果assert后的条件为假,那么将会抛出AssertionError
  
  后面的参数可选,表示当抛出异常时说的话(没文化,词穷)

  举例:
  

  1. >>>assert 1+1 == 2

  2. >>>assert 1+1 > 2
  3. Traceback (most recent call last):
  4.   File "<pyshell#6>", line 1, in <module>
  5.     assert 1+1>2
  6. AssertionError
  7. >>>assert 1+1 < 2, "1+1不小于2"
  8. Traceback (most recent call last):
  9.   File "<pyshell#8>", line 1, in <module>
  10.     assert 1+1 < 2, "1+1不小于2"
  11. AssertionError: 1+1不小于2
复制代码


  小练习:
  编写一个Python函数,该函数接收一个整数列表作为参数。
  函数的目标是确保列表中的所有元素都满足以下两个条件:
      1.所有元素都是正整数。
      2.列表中的元素按升序排列。
  如果列表满足这两个条件,函数应返回True;否则,应通过assert语句引发AssertionError。
  要求:
    使用assert语句来检查每个条件。
    如果任何一个条件不满足,assert语句应提供清晰的错误信息,指出是哪个条件未满足。
   示例:
  

  1. def check_list(lst):
  2.     # 你的代码 here

  3. # 测试代码
  4. try:
  5.     check_list([1, 2, 3, 4, 5])  # 应该通过,不引发异常
  6.     print("Test 1 Passed")
  7. except AssertionError:
  8.     print("Test 1 Failed")

  9. try:
  10.     check_list([1, 3, 2, 4, 5])  # 应该失败,因为不是升序
  11.     print("Test 2 Passed")
  12. except AssertionError as e:
  13.     print(f"Test 2 Failed: {e}")

  14. try:
  15.     check_list([-1, 2, 3, 4, 5])  # 应该失败,因为包含负数
  16.     print("Test 3 Passed")
  17. except AssertionError as e:
  18.     print(f"Test 3 Failed: {e}")
复制代码

   
    答案:
游客,如果您要查看本帖隐藏内容请回复


不放回帖隐藏了,不然太封闭了

评分

参与人数 1鱼币 +5 收起 理由
某一个“天” + 5

查看全部评分

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2025-1-27 13:53:37 | 显示全部楼层
看看
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-6 02:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表