Fix Python – List comprehension rebinds names even after scope of comprehension. Is this right?

Comprehensions show unusual interactions with scoping. Is this the expected behavior?
x = “original value”
squares = [x**2 for x in range(5)]
print(x) # Prints 4 in Python 2!

At the risk of whining, this is a brutal source of errors. As I write new code, I just occasionally find very weird errors due to rebinding — even now that I know it’s a p….

Fix Python – Python – ‘ascii’ codec can’t decode byte

I’m really confused. I tried to encode but the error said can’t decode….
>>> “你好”.encode(“utf8”)
Traceback (most recent call last):
File ““, line 1, in
UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe4 in position 0: ordinal not in range(128)

I know how to avoid the error with “u” prefix on the string. I’m just wondering….

Fix Python – Copy file with pathlib in Python

I try to copy a file with pathlib
import pathlib
import shutil

my_file=pathlib.Path(‘/etc/hosts’)
to_file=pathlib.Path(‘/tmp/foo’)
shutil.copy(my_file, to_file)

I get this exception:
/home/foo_egs_d/bin/python /home/foo_egs_d/src/test-pathlib-copy.py
Traceback (most recent call last):
File “/home/foo_egs_d/src/test-pathlib-copy.py”, line 6, in….

Fix Python – How to reinstall python@2 from Homebrew?

I have been having issues with openssl and python@2 with brew, which have explained here (unresolved). The documented workaround to reinstall Python and openssl was not working, so I decided I would uninstall and reinstall Python.
The problem is, when you try to install Python 2 with brew, you receive this message:
brew install python@2
Error: No ….

Fix Python – Why does Python print unicode characters when the default encoding is ASCII?

From the Python 2.6 shell:
>>> import sys
>>> print sys.getdefaultencoding()
ascii
>>> print u’\xe9′
é
>>>

I expected to have either some gibberish or an Error after the print statement, since the “é” character isn’t part of ASCII and I haven’t specified an encoding. I guess I don’t understand what ASCII being the default encoding means.
EDIT
I ….