[Tutor] Question regarding syntax

Andre Roberge andre.roberge at gmail.com
Wed Jul 11 21:21:47 CEST 2007


On 7/11/07, Eric Brunson <brunson at brunson.com> wrote:
> Michael Klier wrote:
> > Dave Kuhlman wrote:
> >
> >> On Wed, Jul 11, 2007 at 11:03:18AM -0400, John Morris wrote:
> >>
> >>> I'm editing some code from Mailman and seeing:
> >>>
> >>> legend = _("%(hostname)s Mailing Lists")
> >>>
> >>>
> >
> > I am no python pro but I guess that funtction _() ist just a wrapper
> > function around gettext.gettext from the gettext module (used for
> > localization). I`ve seen that in lots of places and I think that`s
> > common practise (don`t beat me if I am wrong ;-)).
> >
> >
> >> Mailman is a great product. But that bit of code is not, I think,
> >> very good code.
> >>
> >
> > Even the python gettext docs [1] use it that way.
> >
> > [1] http://docs.python.org/lib/node732.html
> >
> >
>
> I've seen it before, too.  From the example in the manual I imagine it's
> more convention than anything hard and fast, probably to make the string
> stand out more than the function call since it's essentially just doing
> a translation.
>

It is a standard convention.  Lots of tools are built on the
assumption that translatable strings are going to be enclosed in
_(...)
These tools extract the strings from programs, and put them in files
(.po) that are easily editable by human translators.

This convention is *not* limited to Python.

André


> However, by assigning gettext.getttext to the local variable '_', you do
> avoid a module lookup every time it's used.  It's useful when you do
> something like this (contrived example):
>
> import math
>
> f = math.cos
> for x in range(0, 100000):
>     print x, f(x)
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list