Given a string that uses named placeholders for formatting, is it possible to inspect what the names are dynamically?
>>> var = '''{foo} {bar} {baz}'''
# How to do this?
>>> for k in var.inspect_named_placeholders():
>>> print(k)
foo
bar
baz
Given a string that uses named placeholders for formatting, is it possible to inspect what the names are dynamically?
>>> var = '''{foo} {bar} {baz}'''
# How to do this?
>>> for k in var.inspect_named_placeholders():
>>> print(k)
foo
bar
baz
Yes, you can use the built-in parser:
>>> var = '''{foo} {bar} {baz}'''
>>> import string
>>> formatter = string.Formatter()
>>> formatter.parse(var)
<formatteriterator object at 0x109174750>
>>> list(formatter.parse(var))
[('', 'foo', '', None), (' ', 'bar', '', None), (' ', 'baz', '', None)]