0

from python documentation :

We can calculate the list of squares without any side effects using:

squares = [x**2 for x in range(10)]

My question is - How is this without side effects ?

the above code also creates (or overwrites) a variable named x that still exists after the loop completes.

Edited : since I've been asked for code samples (this is from python 2.7)

x = 0
print "before list comprehension"
print "x = "+str(x)
squares = [x**2 for x in range(10)]
print "after list comprehension"
print "x = "+str(x)

Output -

before list comprehension
x = 0
after list comprehension
x = 9
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Thalish Sajeed
  • 1,351
  • 11
  • 25

0 Answers0