Python list comprehension overriding value

后端 未结 4 1533
小蘑菇
小蘑菇 2020-11-30 13:58

have a look at the following piece of code, which shows a list comprehension..

>>> i = 6
>>> s = [i * i for i in range(100)]
>>> p         


        
4条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-30 14:09

    Yes, there is a reason, and the reason is that they didn't want the temporary variable in a list comprehension to leak into the outer namespace. So it is an intentional change that is a result of list comprehensions now being syntactic sugar for passing a generator expression to list().

    Ref: PEP3100.

提交回复
热议问题