On 7/11/07, Dave Kuhlman <[EMAIL PROTECTED]> 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")
>

The outer parentheses are a function call.  The underscore
is a name that has a callable as a value, I suppose.  I
believe that the value of the name underscore is the last
expression evaluated, but I'm not sure.


Right... Thanks, I figured it was something like that but it was not
something I'd encountered.
so if _ =  foo
then
bar = _("test") is equivalent to
bar = foo("test")


Mailman is a great product.  But that bit of code is not, I think,
very good code.  In Python explicitness is a virtue, and the use of
the underscore is implicit and is not very Pythonic.


Agreed. The _ stuff is reminiscent of Perl  $_, @_ and friends. I'd go miles
personally to
avoid that usage, personally.

I have done the whole 'import this' and mightily strive to grok it all
properly on a regular basis. ;-)

By the way, The inner parentheses are a formatting operation.
%(x)s will be replaced by the value of x in Example:

   vals = {'animal': 'dog'}
   "Rover is a big %(animal)s." % vals

"%(animal)s" will be replaced by "dog".  When you use this form,
the value on the right of the formatting operator must be a
dictionary.  More from the library reference:

    When the right argument is a dictionary (or other mapping type),
    then the formats in the string must include a parenthesised mapping
    key into that dictionary inserted immediately after the "%"
    character. The mapping key selects the value to be formatted from
    the mapping. For example:

    >>> print '%(language)s has %(#)03d quote types.' % \
              {'language': "Python", "#": 2}
    Python has 002 quote types.

          -- http://docs.python.org/lib/typesseq-strings.html


Thanks for this too, though it's more completeness than I needed (just
wondered if _( was "special" usage or what. Kudos on an excellent reply.

So, any really good tutorials on FP and map, filter, zip, lambda ?
I'm trying to wrap my mind around those better...


Thanks much!
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to