gpt4 book ai didi

python - AttributeError : 'module' object has no attribute 'lock'

转载 作者:行者123 更新时间:2023-11-28 17:36:38 24 4
gpt4 key购买 nike

作为我的单元测试程序的一部分,我正在评估从某个方法返回的变量的类型。该方法返回一个类型为“thread.lock”的变量,我想以与测试类型为“str”或“int”等的变量相同的方式对此进行测试

This is the example carried out in the python 2.7.6 shell

>>> import threading, thread
>>> mylock = threading.Lock()
>>> type(mylock)
<type 'thread.lock'>
>>> type(mylock) is thread.lock

Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
type(mylock) is thread.lock
AttributeError: 'module' object has no attribute 'lock'

I expected it to return True as shown in the second example

>>> myint = 4
>>> type(myint)
<type 'int'>
>>> type(myint) is int
True
>>>

请提供有关如何解决此问题的任何解决方案,我们将不胜感激。谢谢

最佳答案

使用thread.LockType代替thread.lock:

>>> import threading, thread
>>> mylock = threading.Lock()
>>> type(mylock)
<type 'thread.lock'>
>>> thread.LockType
<type 'thread.lock'>
>>> type(mylock) is thread.LockType
True

但最好使用isinstance():

>>> isinstance(mylock, thread.LockType)
True

关于 python - AttributeError : 'module' object has no attribute 'lock' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29820257/

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