Immutable object thread-safety

Gabriel Genellina gagsl-py2 at
Mon Oct 27 08:21:40 CET 2008

En Sun, 26 Oct 2008 23:25:09 -0200, Alcari The Mad  
<AlcariTheMad at> escribió:

>> I am confused about which data structure to rely on thread-safety, or
>> operator in Python?
> All of the builtin functions(which are implemented in C, like len()) are
> atomic(but assigning their output to a value may not be).

You can't count on the builtins being atomic. len(x) executes  
type(x).__len__ if such method exists, which may execute arbitrary Python  
code, even trigger the garbage collector and run absolutely unrelated  
See this effbot page for discussion [1] - but in general, since the  
language reference doesn't specify whether an operation is atomic or not,  
you should not count on it. Use a lock when required.


Gabriel Genellina

More information about the Python-list mailing list