|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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}")
答案:
不放回帖隐藏了,不然太封闭了 |
评分
-
查看全部评分
|