On Wed, Oct 21, 2020 at 06:17:27AM +1100, Chris Angelico wrote:

> No; it can fail if the pattern actually rejects it. For instance, a
> pattern of "a{x}b{y}c" can match the string "a" but won't assign to y,

It won't assign to x either, if I'm understanding it correctly, since 
there is nothing following the 'a' that matches. Or will x get the empty 
string?


> however it can't match the string "q" because that doesn't match. This
> behaviour of leaving variables unassigned happens ONLY if there's a
> partial match.
> 
> And just like with a regex, it would be easy enough to have a notation
> that demands end-of-string, and cannot match unless you reach that
> point in the pattern right as you reach the end of the string.

But you'll still get a partial match.

Using regex syntax, if you try to match "spam {x} eggs$" (i.e. the 
prefix "spam", space, substring to be extracted, space, suffix "eggs", 
end of string) against "spam is great" you will get the partial match 
x="is".

If you're trying to match the address "{num:d} Main Street {city}$" 
against "3.1415" you will get num=3. Maybe I'm missing something, but I 
don't see why this is desirable behaviour.


[...]
> Well... yes. And oddly enough, developing a scanning mini-language
> inspired by the formatting language is *exactly* what this proposal
> is, and has always been.

That's incorrect.

The first post in this thread:

https://mail.python.org/archives/list/python-ideas@python.org/thread/JEGSKODAK5MCO2HHUF4555JZPZ6SKNEC/

literally describes it as f-strings as the target. There's no hint that 
only a subset of f-string functionality will be accepted. Dennis says:

"you can assign a string to an f-string"

and proposes an exception:

"ValueError: f-string assignment target does not match ..."

I can't work out how to see messages in non-threaded date order on the 
website, but it looks to me like the first suggestion that this might 
not *literally* be f-strings comes about a dozen emails into the thread, 
and from you, not Dennis.

The subject line doesn't say "Scanning mini-language inspired by 
f-strings", it says that f-strings should be allowed as an assignment 
target. I think that people can be forgiven for thinking that a thread 
that says it is about making f-strings assignment targets is about 
making f-strings assignment targets *wink*

I think we have at least six mini-languages now?

- regexes
- string interpolation with %
- template strings
- format mini-language
- f-strings
- date/time format strings

I think it would be quite an uphill battle to have a seventh built into 
the language. You're welcome to write a PEP :-)


-- 
Steve
_______________________________________________
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/PARFBZBPAQXLJ6XRV7LY67IVYMS7HNJC/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to