3 ms·
Python 3 fixes the issue where variables in a comprehension "leak" to the surrounding scope. Python 2: x = 'hello' [x*x for x in range(5)] #=> [0, 1,
by andreareina 10y ago
Python 3 fixes the issue where variables in a comprehension "leak" to the surrounding scope.
Python 2:
x = 'hello'
[x*x for x in range(5)] #=> [0, 1, 4, 9, 16]
x #=> 4
Python 3:
x = 'hello'
[x*x for x in range(5)] #=> [0, 1, 4, 9, 16]
x #=> 'hello'
Python 3 has improved destructuring support
Python 2:
a, *b = [1, 2, 3] #=> Syntax error
Python 3:
a, *b = [1, 2, 3]
a #=> 1
b #=> [2, 3]
Type annotations[1] are nice not because they enable typechecking, but because they tell me what the function expects. Maybe it's just the kind of programming I do, but I use a bunch of libraries with functions that take a class or function as input, except half the time they want the name to the thing instead of the actual reference.
[1] https://www.python.org/dev/peps/pep-0484/ https://www.python.org/dev/peps/pep-0484/