Problem with mixing doctest with gettext _()

Pierre Rouleau prouleau at impathnetworks.com
Fri Feb 27 17:29:57 EST 2004


>>
>>
>>You're right!  It does work. I must admit, that I don't see why though.
>>  (a,) makes a tuple out of the `a` argument.  Does the %r conversion
>>require a tuple?
> 
> 
> The formatting operator behaves differently depending on whether the
> righthand argument is a tuple or something else. 
> 
> formatstr % tuple
> 
> ensures that there is a corresponding format expression - e.g. "%s" or "%d"
> - for every item in the tuple, whereas
> 
> formatstr % nontuple 
> 
> expects exactly one format expression. Therefore
> 
> "%r\n" % a
> 
> raises an exception when a is a tuple with more or less than one item and
> wrongly prints the item's representation instead of the tuple's for
> one-tuples.
> Wrapping it like so (a,) fixes the problem because now we have always a
> tuple with one item - where this item is sometimes a tuple.
> 
> An alternative approach would be to ensure that the righthand side is always
> a nontuple:
> 
> "%s\n" % repr(a)
> 

Thanks for this clear explanation!

Pierre



More information about the Python-list mailing list