String formatting with the format string syntax
Peter Otten
__peter__ at web.de
Wed Sep 15 04:48:35 EDT 2010
Andre Alexander Bell wrote:
> On 09/15/2010 10:00 AM, Peter Otten wrote:
>> def extract_names(t, recurse=1):
>> for _, name, fmt, _ in t._formatter_parser():
>> if name is not None:
>> yield name
>> if recurse and fmt is not None:
>> for name in extract_names(fmt, recurse-1):
>> yield name
>
> Thanks Peter, I very much like this generator solution. It will work for
> all situations I can currently think of.
>
> However, one thing remains. It is based on the _format_parser method.
> And as I wrote in my original post this one - since it starts with _ -
> suggests to me to better not be used. So if using this method is
> completely ok, why does it start with _, why is it almost undocumented?
> Or did I miss something, some docs somewhere?
Sorry, I really should have read your original post carefully/completely. It
would have spared me from finding _formatter_parser() independently...
I personally would not be too concerned about the leading underscore, but
you can use
string.Formatter().parse(template)
instead.
Peter
More information about the Python-list
mailing list