string formatting with mapping & '*'... is this a bug?

Jeff Epler jepler at unpythonic.net
Fri Sep 10 11:42:08 EDT 2004


On Fri, Sep 10, 2004 at 08:27:44AM +0200, Alex Martelli wrote:
> Pierre Fortin <pfortin at pfortin.com> wrote:
>    ...
> > I was hoping to use the likes of:  "%(key)*.*f" % map
> > however, unlike with the non-(key) formats, there appears to be no way to
> > specify a "*.*" size when a map is used...
> 
> It is not clear to me where you would expect to get the values for those
> stars from -- 'map' being a dictionary it has no concept of "ordering",
> so there is no concept of "next value" after map['key'].

Perhaps Python string formatting should be enhanced similarly to the $
extension (from SuS) in C.
       By default, the arguments are used in  the  order given,  where
       each `*' and each conversion specifier asks for the next argument
       (and it is an  error  if  insufficiently  many  arguments  are
       given).   One  can  also specify explicitly which argument is
       taken, at each place where an argument is required, by writing
       `%m$'  instead  of `%'  and  `*m$' instead of `*', where the
       decimal integer m denotes the position in the argument list of
       the desired argument, indexed starting from 1. Thus,
                   printf("%*d", width, num);
       and
                   printf("%2$*1$d", width, num);
       are equivalent.
In the case of Python, this would become
        print "%(width)*(num)d" % {'width': width, 'num': num}

Jeff
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20040910/b8ec2540/attachment.sig>


More information about the Python-list mailing list