We have a large project that is entirely coded in ASCII. Is it worth putting coding statements at the beginning of each source file (e.g. #coding=utf-8) for some reason if the source doesn't have any unicode in it?
Thanks, --Peter
For portability I would explicitly declare it, especially as the default file encoding is changing in Python 3 (see PEP-3120):
This PEP proposes to change the default source encoding from ASCII to UTF-8. Support for alternative source encodings continues to exist; an explicit encoding declaration takes precedence over the default.
Although it doesn't affect you with ASCII, seeing how explicit is better than implicit I would recommend you add it to the top of your file.
ASCII is the default in Python 2. UTF-8 is the default in Python 3.
If your files are ascii-only; you don't need to declare the source code encoding in both version (ascii is a subset of utf-8).
Non-ASCII character leads to SyntaxError in Python 2 therefore an accidental non-ascii character won't go unnoticed and won't corrupt any data. There is no reason to declare source code encoding for ascii-only files.
You should do one of two things (at least):
You might want to check if you get significantly better startup when the explicit tag is UTF-8 though. Anyway, I would consider that a bug of the interpreter.
This way, if anyone slips and mistakenly adds some non-ASCII characters, you won't have to chase that (potential) bug. Explicitly restricting to ASCII has one advantage: You actually can reliably see what each string contains and there are no equal-seeming distinct names.
#coding=utf8to the top would make it confusing if theres no utf8 characters ... (not overly so but meh ...)