gpt4 book ai didi

python - 当向 uuid.UUID() 提供无效参数时会发生什么?

转载 作者:行者123 更新时间:2023-11-28 20:25:25 28 4
gpt4 key购买 nike

会抛出异常吗? UUID() 是否会悄无声息地失败?是否有任何情况下“myStatus”来自

myStatus = True
myUUID = uuid.UUID( someWeirdValue )
if myUUID == None:
myStatus = False

是否等于 False?

最佳答案

UUID() 构造函数会引发 TypeErrorValueError,具体取决于传入的内容。

不传递任何 hexbytesbytes_lefieldsint 选项引发 TypeError,传入无效值引发 ValueError:

>>> uuid.UUID()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/uuid.py", line 129, in __init__
raise TypeError('need one of hex, bytes, bytes_le, fields, or int')
TypeError: need one of hex, bytes, bytes_le, fields, or int
>>> uuid.UUID('abcd')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/uuid.py", line 134, in __init__
raise ValueError('badly formed hexadecimal UUID string')
ValueError: badly formed hexadecimal UUID string
>>> uuid.UUID(bytes='abcd')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/uuid.py", line 144, in __init__
raise ValueError('bytes is not a 16-char string')
ValueError: bytes is not a 16-char string

等等

不会默默地失败。它肯定永远不会返回 NonemyUUID 设置为 UUID 实例,或者引发异常。

关于python - 当向 uuid.UUID() 提供无效参数时会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15278467/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com