How safe is modifying locals()?

Steven Taschuk staschuk at telusplanet.net
Fri Jul 25 17:32:48 EDT 2003


Quoth Paul Paterson:
  [...]
> Does anyone have any alternative strategies for trying to achieve the 
> objective? A "Pythonic" approach such as,
> 
> def change(x, y):
>     x = x + 1
>     y = y + 1
>     return y
> 
> x = 0; y = 0
> y = change(x, y)
> 
> Works in a single threaded environment but, because the value of 'y' 
> changes at the wrong time, with multiple threads there is a possibility 
> that two users of 'y' could dissagree on its value.

Even if the simple
    y = y + 1
could change the caller's binding, there would be a race condition
-- you'll need a lock anyway.  (I'd be a bit surprised if this
were not so in VB as well.  Are statements atomic in VB?)

-- 
Steven Taschuk                                                   w_w
staschuk at telusplanet.net                                      ,-= U
                                                               1 1





More information about the Python-list mailing list