[Python-ideas] Briefer string format
Steven D'Aprano
steve at pearwood.info
Sat Jul 25 04:37:29 CEST 2015
On Fri, Jul 24, 2015 at 11:40:49AM +1200, Greg Ewing wrote:
> Steven D'Aprano wrote:
>
> >I don't think I want this behaviour:
> >
> > f'{spam}' '{eggs}'
> > => format(spam) + '{eggs}'
>
> What do you think should happen in this case:
>
> '{spam}' f'{eggs}'
As I stated before, I think that should at least raise a warning, if not
a syntax error. I think we're in uncharted territory here, because f
strings aren't really a literal string, they're actually a runtime
function call, and we haven't got much in the way of prior-art for
implicit concatenation of a literal with a function call. So we ought to
be cautious when dealing with anything the least bit ambiguous, and
avoid baking in a mistake that we can't easily change.
There's no ambiguity with concat'ing f strings only, or non-f strings
only, but the more we discuss this, the more inclined I am to say that
implicit concatenation between f strings and non-f strings *in any
order* should be a syntax error.
> It would seem very strange to me if the f infected
> strings *before* it as well as after it.
It's consistent with Python 2:
py> "abc" u"ßŮƕΩж" "def"
u'abc\xdf\u016e\u0195\u03a9\u0436def'
The unicodeness of the middle term turns the entire concatenation into
unicode. I think it is informative that Python 3 no longer allows this
behaviour:
py> b"abc" u"ßŮƕΩж" b"def"
File "<stdin>", line 1
SyntaxError: cannot mix bytes and nonbytes literals
--
Steve
More information about the Python-ideas
mailing list