会抛出异常吗?UUID() 是否会默默地失败?是否有任何情况下'myStatus'来自
myStatus = True
myUUID = uuid.UUID( someWeirdValue )
if myUUID == None:
myStatus = False
等于假?
根据传入的内容,UUID()
构造函数会引发 aTypeError
或 a 。ValueError
不传入任何hex
, bytes
, bytes_le
, fields
, 或int
选项会引发 a TypeError
,传入无效的值会引发 a 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
等等
它不会默默地失败。它肯定永远不会回来None
。要么myUUID
设置为UUID
实例,要么引发异常。