看下面兩個例子:python
python2的環境:列表中的同名變量名被替換了blog
>>> x = 'my precious' >>> dummy = [x for x in 'ABC'] >>> dummy ['A', 'B', 'C'] >>> x 'C'
python3:列表中的同名變量名沒有被替換ci
>>> x = 'ABC' >>> dummy = [ord(x) for x in x] >>> x 'ABC' >>> dummy [65, 66, 67]
總結:在python3環境,對於列表推導、生成器表達式、字典推導、set推導,python3會維護本身的局部變量,局部變量不會影響上下同名的變量;io