3 ms·
For point 10: '_' is often aliased as gettext to ease translation of string: from django.utils.translation import ugettext as _ translated_str = _('S
by Walkman 13y ago
For point 10:
'_' is often aliased as gettext to ease translation of string:
from django.utils.translation import ugettext as _
translated_str = _('Something to translate')
so using it will overwrite the alias. Instead, you can use '__' (double underscore) as ncoghlan suggests below his answer [1].
or you can use the 'unused_' prefix as Google Python Style Guide suggests [2]
or you can change your code, so you don't need to use '_' as Alex Martelli suggests in his answer [3].
[1]: http://stackoverflow.com/a/5893946/720077 http://stackoverflow.com/a/5893946/720077
[2]: http://google-styleguide.googlecode.com/svn/trunk/pyguide.html?showone=Lint#Lint http://google-styleguide.googlecode.com/svn/trunk/pyguide.ht...
[3]: http://stackoverflow.com/a/1739541/720077 http://stackoverflow.com/a/1739541/720077
- falcolas 13y agoI have personally never seen this before. I'd be wary to use it, since it breaks the "_ is a throwaway" idiom, as well as the REPL "_ is the results of the last expression" function. Aliasing it to "t" or "txl" seems like a saner way, if I'm honest.
- Walkman 13y agoyou never use gettext in REPL :) and it's common to use '_' as gettext. Less typing, more readable strings. If you don't believe me, you should believe Alex Martelli [1], a random Python developer [2] or just read Official Python documentation [3] which recommends assignign gettext.gettext to '_'... [1]: http://stackoverflow.com/a/2745537/720077 http://stackoverflow.com/a/2745537/720077 http://stackoverflow.com/a/1739541/720077 http://stackoverflow.com/a/1739541/720077 [2]: http://stackoverflow.com/a/5893946/720077 http://stackoverflow.com/a/5893946/720077 [3]: http://docs.python.org/2/library/gettext.html http://docs.python.org/2/library/gettext.html
- thruflo 13y agoThis has been established practise in Zope, Plone et al for many years. http://developer.plone.org/i18n/internationalisation.html#marking-translatable-strings-in-python http://developer.plone.org/i18n/internationalisation.html#ma...
- dalke 13y agoIt's standard Python practice since Martin von Löwis's work in support for internationalization, which he presented at the IPC6 Python conference in 1997. Quoting from http://www.python.org/workshops/1997-10/proceedings/loewis.html http://www.python.org/workshops/1997-10/proceedings/loewis.h... : > Here are several ways of producing the message catalogs. This is the one suggested by the GNU gettext documentation. First, all translatable message strings in the source code must be marked. In order to disturb readability as little as possible, the wrapper function around each string should be called _ (underscore). This use of the underscore usually does not interfere with its meaning in the interactive mode. A gettextized module would then begin with import intl _=intl.gettext
- collyw 13y agoI got caught out by this recently. I wasn't expecting it to be used as a method alias and assumed it was an unusual part of Pythons syntax that I hadn't discovered. I had to get the answer from Stack Overflow. Not very intuitive.