4 ms·
So, you don't use open("w"), do you ? You use the non magic open(os.O_WRONLY) of course. Because magic strings are bad.
by BiteCode_dev 6y ago
So, you don't use open("w"), do you ? You use the non magic open(os.O_WRONLY) of course.
Because magic strings are bad.
- comfydragon 6y agoPython 3.8 has typing.Literal for this.
- MikeTheGreat 6y agoI will now - thanks for the tip! :) And, moreover, this will inspire me to ask "How many other traditional magic numbers/strings/etc have I gotten used to that might be defined more readably?" strcmp, I'm looking at you! I know your post was sarcastic/snarky but it was genuinely helpful
- 1wd 6y agof.tell(offset, os.SEEK_CUR) over f.tell(offset, 1) https://docs.python.org/3/library/os.html#os.SEEK_CUR https://docs.python.org/3/library/os.html#os.SEEK_CUR I wish the docs on f.tell at least mentioned the option https://docs.python.org/3/tutorial/inputoutput.html#methods-of-file-objects https://docs.python.org/3/tutorial/inputoutput.html#methods-...
- remram 6y agoExcept it wasn't, os.O_WRONLY is for os.open() (equivalent of open syscall, takes numeric flags), not the built-in open() (equivalent of C fopen, takes "w" and similar strings).
- 1wd 6y agoYou would have to use os.open
- BiteCode_dev 6y agoI would also have to pass the path as a first param. This was not meant to be correct, just teasy.
- zo1 6y agoThat's a fair point, and I'll be sure to keep an eye out for that type of magic-string usage going forward. Though most "magic string" usages are not nearly as official as the one you mention and usually occur in business-level code written by ordinary developers that may not be disciplined or experienced. Those spots most certainly need all the assistance they can get instead of relying on magic-strings, hence my comment of magic strings being bad.