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