On Fri, Sep 18, 2020, 8:17 AM Ricky Teachey <ricky@teachey.org> wrote:
Why not just grow a parse method on str that returns a dict and do it this way?
q = "{a} {b}"
p = "1 2"(a, b) = q.parse(p)
Sorry that should have been:
(a, b) = q.parse(p).values()
I don't understand why returning a dict is useful. Unless this becomes as complicated as regexes with grouping, etc. (in which case: use a regex), the values will be returned in the order they appear in the template string. So just have it return a tuple:
a, b = q.parse(p)
Or for something that could be written today:
a, b = parse("{:d} {:d}", "1 2")
assert a == 1 and b == 2
I don't see the need for new syntax or new ways to assign values. You're not even removing any duplication compared to:
f"{a:d} {b:d}" = "1 2"
Although I grant you'd be saving a few characters of typing. But
there's no DRY advantage to be gained. And you wouldn't be
restricted to using string literals for the template.
If it got super popular, make parse() a method on str.
As near as I can tell, all of these proposals would require type checkers to be modified to understand them, but that's the nature of the beast when you have the type of something dependent on the contents of a string. They would have to figure out that a and b are int's in my example.
So, still -1 on any special syntax for this.
Eric