I'm currently switching a codebase
over from python2 to python3 and come across an issue I don't quite understand :
class MyClass:
var1 = True
var2 = tuple([i for i in [1, 2,] if var1])
The above class was running quite happily in python2, but broke in python3.
I changed it to :
class MyClass:
var1 = True
var2 = tuple(i for i in [1, 2,] if var1)
because my understanding was the list comprehension was redundant, regardless it doesn't work. after some investigating it appears that list/tuple
comprehensions behave in a way I don't quite understand when they're in the body of a class being initialized.
# Breaks in python 2 and 3
class MyClass:
var1 = True
var2 = tuple(i for i in [1, 2,] if var1)
# Works in python 2, breaks in 3
class MyClass:
var1 = True
var2 = [i for i in [1, 2,] if var1]
Any pointers on what's going on?