Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

As part of my unit testing procedure i'm evaluating the type of a variable which has been returned from a certain method. The method returns a variable of type 'thread.lock' and I would like to test for this in the same way I test for variables of type 'str' or 'int' etc

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
>>> 

Please any solutions on how to get around this will be highly appreciated. Thanks

like image 374
BeeLabeille Avatar asked Oct 14 '25 04:10

BeeLabeille


1 Answers

Instead of thread.lock use thread.LockType:

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

But it is preferable to use isinstance():

>>> isinstance(mylock, thread.LockType)
True
like image 88
mhawke Avatar answered Oct 16 '25 18:10

mhawke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!