This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Modify Formatter Class to handle arbitrary objects
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.10
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Ross Biro, eric.smith, lisroach
Priority: normal Keywords:

Created on 2019-03-01 18:00 by Ross Biro, last changed 2022-04-11 14:59 by admin.

Messages (4)
msg336945 - (view) Author: Ross Biro (Ross Biro) Date: 2019-03-01 18:00
The only point in the string.Formatter class that really depends on string output is line 245: return ''.join(result), auto_arg_index.  

I'm currently working on a problem that would be much easier if I could get access to the result list instead of having that join called.

I propose creating another overridable method in string.Formatter, say oformat that calls _vformat and is called by vformat. oformat would have the guts of vformat and just return the result list.  vformat would then consist of the call ot oformat and the join.  See Below.  The recursive call to _vformat would also need some slight modification.

The work would not be difficult and I'm willing to do it, provided there is sufficient interest.

def vformat(self, format_string, args, kwargs):
        result = self.oformat(format_string, args, kwargs)
	return ''.join(result)

def oformat(self, format_string, args, kwargs):
        used_args = set()
        result, _ = self._vformat(format_string, args, kwargs, used_args, 2)
        self.check_unused_args(used_args, args, kwargs)
        return result
msg337715 - (view) Author: Lisa Roach (lisroach) * (Python committer) Date: 2019-03-12 03:47
Can you give an example use case for this? F-strings are the newer method of string interpolation, I'm not sure it's worth putting effort into adding features to string.Formatter.
msg337738 - (view) Author: Ross Biro (Ross Biro) Date: 2019-03-12 13:50
I'm currently writing a language translator between two domain specific
computer languages.  Because some expressions occur repeatedly, but in
slightly different contexts, I make multiple passes.  The first pass
reduces everything it can and leaves place holder objects for things it
can't reduce.  Later passes replace the objects with their final expression
in the new language.  The final expression varies by context, so every time
it's reevaluated, it could change.  I would really like to handle things
like a + b as

"{a} + {b}".format(a=a, b=b)

This works great when a and b are strings.  But when they are place holder
objects, I wasn't able to find a good solution.  Although the Formatter
class came so close that I thought I would suggest the change.  What I
ended up doing was replacing objects with unique strings so that I could
use format and then using regular expressions on the output string to split
it into an array and replace the string identifiers with the original
objects.  The change I've suggested to the Formatter class would have
allowed me to skip the regular expressions.


On Mon, Mar 11, 2019 at 11:47 PM Lisa Roach <> wrote:

> Lisa Roach <> added the comment:
> Can you give an example use case for this? F-strings are the newer method
> of string interpolation, I'm not sure it's worth putting effort into adding
> features to string.Formatter.
> ----------
> nosy: +lisroach
> _______________________________________
> Python tracker <>
> <>
> _______________________________________

*Ross Biro* | CTO

O: 240-380-2231|  F: 240-556-0361 <(240)%20556-0361>
The Interface Financial Group <>

CONFIDENTIALITY NOTICE: This email message, including any attachments, is
for the sole use of the intended recipient/s and may contain confidential &
privileged information. Any unauthorized review, use, disclosure, or
distribution is prohibited. If you are not the intended recipient, please
contact the sender by reply email and destroy all copies of the original
messages and any attachments.
msg381751 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-11-24 15:34
I just stumbled across this. I don't think the idea is totally without merit, although maybe it's too niche to warrant being in the stdlib. It should probably be discussed on python-ideas first.
Date User Action Args
2022-04-11 14:59:11adminsetgithub: 80340
2020-11-24 15:34:57eric.smithsetmessages: + msg381751
versions: + Python 3.10, - Python 2.7, Python 3.8
2019-03-12 13:50:01Ross Birosetmessages: + msg337738
2019-03-12 03:47:09lisroachsetnosy: + lisroach
messages: + msg337715
2019-03-01 19:38:04xtreaksetnosy: + eric.smith
2019-03-01 18:00:37Ross Birocreate