3 ms·
> With tuple unpacking, using _ as a throwaway label is also OK. I see this sometimes, but the problem is that this breaks internationalization using gettext,
by teddyh 11y ago
> With tuple unpacking, using _ as a throwaway label is also OK.
I see this sometimes, but the problem is that this breaks internationalization using gettext, commonly imported under the name _:
from gettext import gettext as _
(For those who’ve never seen it, “_” is then used as a function which wraps strings, like _("Book"); translation tools can then parse the source code, deduce that “Book” is a term which needs translating, and at runtime the “_” function can return the translated term instead of the original string when a different language is needed. The name “_” for this functionality predates Python and is a common feature across most programming language using the gettext method of internationalization. It is therefore unfortunate that some Python users have taken “_” to be a kind of throwaway variable.)
- Spiritus 11y agoI thought it was mostly imported as __ (double underscore).
- teddyh 11y agoNo, I’ve never seen that. The normal single-underscore name is used in the Python documentation: https://docs.python.org/3.6/library/gettext.html https://docs.python.org/3.6/library/gettext.html
- Spiritus 11y agoYou're right, I was probably thinking of some other library/framework/language.
- deleted 11y ago[deleted]
- scardine 11y agoThis is true, but any decent IDE (I use PyCharm by Jetbrains) will hint you are overrinding a global variable. I like to use the underscore as a throwaway because it is explicit - it is far more common for me to override other common throwaway variable names like "i", "j" or "x" that are also often used as loop counters.
- oinksoft 11y agoDo those editors use the project's builtin namespace? gettext.install sets __builtins__._ to the translation function so that individual modules don't have to import it.
- batat 11y agoYep, there's an option "Collect run-time types information for code insight" (not sure if enabled by default). JetBrains IDEs are smart as hell.
- deathanatos 11y agoI abhor this practice, frankly. It doesn't take that much more work to type gettext("The translating string"), or even t("string"). The purpose of "blank variable" is better served by _ than translations are. And gettext the tool can pull out the terms requiring translation using any identifier; you don't have to use "_", and I believe "gettext" is supported automatically. Also, depending on what you're doing, a simple function might not be enough; the _("foo") stuff was intended to simply use the locale of the local machine, which is almost certainly wrong in certain environments, such as server-side programming. (Where we need to bind in things like the user's preferences, or the Accept-Language header on the current request to the call to the translation function, so _("foo") is really something like _("foo", request), though with a bit of magic we can get back to a _("foo") syntax, though again, I prefer the explicitness of gettext("foo") or translate("foo").)