-
-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modify Formatter Class to handle arbitrary objects #80340
Comments
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 |
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. |
I'm currently writing a language translator between two domain specific "{a} + {b}".format(a=a, b=b) This works great when a and b are strings. But when they are place holder
On Mon, Mar 11, 2019 at 11:47 PM Lisa Roach <report@bugs.python.org> wrote:
-- O: 240-380-2231| F: 240-556-0361 <(240)%20556-0361> CONFIDENTIALITY NOTICE: This email message, including any attachments, is |
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. |
I'm going to close this, although I still think the idea has some merit. If someone wants to pick it up, I think bringing it up on discuss.python.org is the next step. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: