[Python-ideas] Gettext syntax (was Re: Allow "assigning" to ...)

Terry Reedy tjreedy at udel.edu
Thu Feb 12 04:33:15 CET 2015


On 2/11/2015 6:01 PM, Nick Coghlan wrote:

> I switched my own dummy variable recommendation to a double-underscore
> years ago for exactly that reason. It (very) mildly irritates me that
> so many sources still recommend the single-underscore that clobbers
> the conventional name for the gettext translation builtin :)

People are just copying python itself, which defines _ as a throwaway 
name.  I suspect it did so before gettext.

By reusing _, gettext made interactive experimentation tricky.

 >>> import gettext
 >>> gettext.install('foo')
 >>> _
<bound method NullTranslations.gettext of <gettext.NullTranslations 
object at 0x000000000361E978>>
 >>> _('abc')
'abc'
 >>> _
'abc'  # whoops

A possible solution would be to give 3.x str a __pos__ or __neg__ 
method, so +'python' or -'python' would mean the translation of 'python' 
(the snake).  This would be even *less* intrusive and easier to write 
than _('python').

Since () has equal precedence with . while + and - are lower, we would 
have the following equivalences for mixed expressions with '.':

_(snake.format()) == -snake.format()  # format, then translate
                # or -(snake.lower())
_(snake).format() == (-snake).format()  # translate, then format

-- 
Terry Jan Reedy



More information about the Python-ideas mailing list