[Tutor] Question regarding syntax

Dave Kuhlman dkuhlman at rexx.com
Wed Jul 11 18:00:35 CEST 2007


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 outter parenthese 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.

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.

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

Dave



-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman


More information about the Tutor mailing list