Is a variable swap guaranteed to be atomic in python?
问题 With reference to the following link: http://docs.python.org/faq/library.html#what-kinds-of-global-value-mutation-are-thread-safe I wanted to know if the following: (x, y) = (y, x) will be guaranteed atomic in cPython. (x and y are both python variables) 回答1: Let's see: >>> x = 1 >>> y = 2 >>> def swap_xy(): ... global x, y ... (x, y) = (y, x) ... >>> dis.dis(swap_xy) 3 0 LOAD_GLOBAL 0 (y) 3 LOAD_GLOBAL 1 (x) 6 ROT_TWO 7 STORE_GLOBAL 1 (x) 10 STORE_GLOBAL 0 (y) 13 LOAD_CONST 0 (None) 16