3 ms·
This is good, but I'd prefer the error to occur at compile time. Consider the case where you do something like creating an error message because you can't open
by jrockway 7y ago
This is good, but I'd prefer the error to occur at compile time. Consider the case where you do something like creating an error message because you can't open a file, and want to include the filename. The filename is bytes, the error message is a string, so instead of being able to print the error message, you instead throw an undebuggable exception.
Dunno if that in particular is a problem in Python or not... but it is the sort of thing to watch out for.
- Sohcahtoa82 7y agoIn Python, filenames can be either bytes or string. Also, I made a mistake in my previous post. File I/O can deal with bytes OR strings depending on the parameters sent to the `open()` function. file_handle = open('somefile.txt') This opens the file in text mode (so `file_handle.read()` returns strings) using a platform-dependent encoding. On Windows, this will probably be CP-1252. On Linux, UTF-8. Of course, you can always explicitly choose the encoding: file_handle = open('somefile.txt', encoding='utf8') If you know you want to only deal with bytes, you can explicitly set that: file_handle = open('somefile.ext', mode='rb') I agree that it'd be nice to be able to see bytes vs strings errors at compile time, but this is difficult or even impossible with a duck-typed interpreted language like Python. IDEs and linters can make a good attempt at it, but they're not perfect.