I don't understand what is happening in this threading code

Tim Roberts timr at probo.com
Tue Jan 22 01:03:54 EST 2008


Matthew Wilson <matt at tplus1.com> wrote:

>In this code, I tried to kill my thread object by setting a variable on it
>to False.
>
>Inside the run method of my thread object, it checks a different
>variable.
>
>I've already rewritten this code to use semaphores, but I'm just curious
>what is going on.
>...
>The memory address of should_keep_running seems to change when I set it
>from True to False, and inside the run method, I keep checking the old
>location.

You misunderstand what "id(a)" does.  "id" doesn't tell you anything about
the name "a" or the address of "a".  What it tells you is the unique
identifier of the object that "a" is bound to.  Any time you change "a",
"id(a)" will also change.

Note, for example:

C:\tmp>python
Python 2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> id(True)
504958236
>>> a = True
>>> id(a)
504958236

>>> id(False)
504958224
>>> a = False
>>> b = False
>>> id(a)
504958224
>>> id(b)
504958224
>>>

504958236 is the id of "True", not of "a".
-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-list mailing list