3 ms·
CORRECTION: My regret was referring to a problem where this code worked in Python 2 but not Python 3: x = 100 y = [x+i for i in range(2)] In Python 3
by riazrizvi 8y ago
CORRECTION: My regret was referring to a problem where this code worked in Python 2 but not Python 3:
x = 100
y = [x+i for i in range(2)]
In Python 3 I kept seeing
NameError: name 'x' is not defined
However, after working around this problem with fixes like this -
x = 100
y = (lambda x=x: [x+i for i in range(2)])()
I have just discovered that the different scope treatment was due to a problem with IPython when using it in embed mode, for example by calling it with the following command:
$ python -c "import IPython;IPython.embed()"
It goes away when you call ipython directly
$ ipython
I mistakenly attributed this problem to Python 3 for years, I guess I introduced the problem because I had a different way of invoking Python 2 and Python 3 - yeesh.