On Fri, Sep 18, 2020 at 11:51 AM David Mertz <me...@gnosis.cx> wrote:
>
> Let's try some others:
>
> >>> my_string = "It might be 11:45 PM"
> >>> # ... many lines later ...
> >>> f"It is {hh}:{mm} {am_or_pm}" = my_string
>
> What are hh, mm, and am_or_pm now?  Do we raise an exception?  Do we run the 
> line having no real idea whether they have been assigned at all? How do we 
> even check whether an assignment took place?
>

You can get this with ANY unpacking form. I don't see how this is any
different. In fact, this kind of usage - where the template is in the
source code but the data comes from a variable - would be the normal
way things would be done, since most text processing involves
something from outside the code. This isn't "action at a distance".
It's perfectly normal coding.

> >>> f"It is {hh}:{mm}!" = "It is 11:45 PM"
>
> Does this work left-to-right? Are hh and mm assigned values, but am_or_pm 
> remains unassigned?

Since am_or_pm wasn't in the template, of course it remains
unassigned. Or is this an error in the example?

It's debatable whether this should assign those it can or bomb if it
can't. I'm inclined towards the "assign those it can" side of things,
since you can always validate with an extra directive, same as you
would use a dollar sign in a regex.

> >>> f"It is {hh}:{mm} {am_or_pm} right now" = "It is 11:45 PM"
>
> Is this a match? Do we only care about the prefix, or does the whole string 
> need to match the inflexible pattern? Obviously, why not use regexen where we 
> can be explicit about such things?
>

As above. I'm slightly in favour of "yes it's a match", but either
works as long as it's a well defined and documented behaviour.

> >>> f"It is {hh}:{mm} {am_or_pm}" = "It is 11:45 PM right now"
>
> Same questions as last, but kinda flipped around? There is no easy intution 
> here, and every behavior will be surprising to most users.

Most? Some perhaps, but that's true of pretty much anything. There are
people confused about *every* behaviour.

>  >>> f"It is {hh()}:{mm()} {am_or_pm()}" = "It is 11:45 PM"
>
> That's a perfectly valid f-string on the LHS. What would you imagine would 
> happen here? If the answer is "raise an exception" then your proposal is 
> actually that some tiny fraction of f-strings could actually be used this 
> way.  That doesn't per se mean the syntax is bad (although it is), but it 
> means that the "symmetries" you hope for between `=` and `==` will hold only 
> for very special cases of f-strings.
>

The syntax for string interpolation is parallel to, but by no means
identical to, the syntax for string parsing. It's just like list
display and unpacking:

# Perfectly valid
spam = [x, y, z[1], z[2]]
[x, y, z[1], z[2]] = range(4)

# Doesn't make sense to unpack this way
spam = [5, ham + eggs]
[5, ham + eggs] = spam

There's a LOT more symmetry than you might think; even if only a small
subset of the syntax is common to both, a huge subset of actual usage
will be.

ChrisA
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/MZK3CCJBOEUKZPLFYAJUBYVWXDMIZ4LX/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to