鱼C论坛

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

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

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

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

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

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

原文:
Help on class AssertionError in module builtins:

class AssertionError(Exception)
 |  Assertion failed.
 |
 |  Method resolution order:
 |      AssertionError
 |      Exception
 |      BaseException
 |      object
 |
 |  Methods defined here:
 |
 |  __init__(self, /, *args, **kwargs)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |
 |  __new__(*args, **kwargs)
 |      Create and return a new object.  See help(type) for accurate signature.
 |
 |  ----------------------------------------------------------------------
 |  Methods inherited from BaseException:
 |
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |
 |  __reduce__(self, /)
 |      Helper for pickle.
 |
 |  __repr__(self, /)
 |      Return repr(self).
 |
 |  __setstate__(self, object, /)
 |
 |  __str__(self, /)
 |      Return str(self).
 |
 |  add_note(self, object, /)
 |      Exception.add_note(note) --
 |      add a note to the exception
 |
 |  with_traceback(self, object, /)
 |      Exception.with_traceback(tb) --
 |      set self.__traceback__ to tb and return self.
 |
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from BaseException:
 |
 |  __cause__
 |      exception cause
 |
 |  __context__
 |      exception context
 |
 |  __dict__
 |
 |  __suppress_context__
 |
 |  __traceback__
 |
 |  args
翻译:
继承自 BaseException 的方法:
 |
 | __getattribute__(self, name, /)
 | 返回 getattr(self,name)。
 |
 | __reduce__(self, /)
 | pickle 的辅助函数。
 |
 | 返回 __repr__(self, /)
 | 返回 repr(self)。
 |
 | __setstate__(self, object, /)
 |
 |__str__(self, /)
 | 返回 str(self)。
 |
 | add_note(self, object, /)
 | Exception.add_note(note) --
 | 为异常添加注释
 |
 | with_traceback(self, object, /)
 | Exception.with_traceback(tb) -- | 为异常添加注释。
 | 将 self.__traceback__ 设为 tb 并返回 self。
 |
 | ----------------------------------------------------------------------
 | 从 BaseException 继承的数据描述符:
 |
 | __cause__
 | 异常原因
 |
 | __context__
 | 异常上下文
 |
 | __dict__
 |
 | __suppress_context__
 |
 | __traceback__
 |
 | args
对于这些异常子类, 很多都是重复的,所以如果重复的没啥意义的我就直接机翻了,不然效率太低,请见谅

用法:

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

  举例:
  
 >>>assert 1+1 == 2

>>>assert 1+1 > 2
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    assert 1+1>2
AssertionError
>>>assert 1+1 < 2, "1+1不小于2"
Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    assert 1+1 < 2, "1+1不小于2"
AssertionError: 1+1不小于2

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

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

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

try:
    check_list([-1, 2, 3, 4, 5])  # 应该失败,因为包含负数
    print("Test 3 Passed")
except AssertionError as e:
    print(f"Test 3 Failed: {e}")
   
    答案:
游客,如果您要查看本帖隐藏内容请回复


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

评分

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

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2025-1-27 13:53:37 | 显示全部楼层
看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-2-24 02:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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