【簡介】python
因爲python-2.x 並無locale這個層次的命名空間,因此臨時變量有可能會泄漏,進而影響到了包涵它的命名空間ui
【看一下pyhont-2.x是怎麼泄漏臨時變量的】spa
python Python 2.7.10 (default, Aug 17 2018, 17:41:52) [GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.0.42)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> x = 100 >>> s = [x for x in range(10) ] >>> x 9 >>>
能夠看到列表推導中用的臨時變量x泄漏致使變局命名空間中的x被污染code
【看一下python-3的狀況】orm
python3 Python 3.6.2 (v3.6.2:5fd33b5926, Jul 16 2017, 20:11:06) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> x = 100 >>> s = [x for x in range(10) ] >>> x 100
能夠看到臨時變量並沒能污染到全局變量、多虧了python-3的locale命名空間blog
【總結】it
因爲 python2 ~ python3 多出了一個locale命名空間,影響是很是深遠的;編寫代碼的時候要多加當心。 io
---form