On Fri, Sep 18, 2020 at 6:19 AM Christopher Barker <pythonchb@gmail.com> wrote:
I like the idea of an scans like ability, but I"m afraid the existing format language is poorly suited to that.
It's simply no designed to be a two-way street:
* ANYTHING can be stringified in Python -- so there is no defined way to turn a string back into a particular type.
OK, so we restrict ourselves to builtins -- how would you reverse this:
In [17]: x, y, z = 23, 45, 67
In [18]: f"{x}{y}{z}" Out[18]: '234567'
so we require a type specifier, but similar problem:
In [19]: f"{x:d}{y:d}{z:d}" Out[19]: '234567'
So we require a field size specifier:
In [20]: f"{x:2d}{y:2d}{z:2d}" Out[20]: '234567'
OK, I guess that is clearly defined. but we've now limited ourselves to a very small subset of the formatting language -- maybe it's not the right language for the job?
And that's why the directives are NOT just a pure mirroring of format string directives. Look at C's scanf and printf functions - they correspond in many ways, but they differ in order to be useful. The point isn't to reverse format(), the point is to have a useful and practical string parser that assigns directly to variables. Also, PEP 622. ChrisA