[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [sc-users] Re: string formatting notation



> But this is adding a new convention for method calls.

It is simply a new kind of literal. Nothing more, nothing less. f is not a method in this design, and nothing requires it to be. I didn't make that clear originally, sorry.

"this is a string" is a literal
3 is a literal
f"this is a ${object}" would be a new kind of literal, just like
u"abc" is a Unicode string literal in C and C++ and
r"\n" is a raw string literal in Python and
0x6 is a hexidecimal number in many languages

'f' in the context of f-strings is a literal prefix that qualifies the interpretation of the literal.

> Would this look up instance vars in the local scope?

I think so, it would be evaluated at runtime. I can imagine that under the hood it would be implemented as a new literal type that compiles to a normal string literal + call to `format`.

> Sorry my python is so rusty, but why would % require you to rewrite
> sentences in % order? If we need to do this in python they're doing %
> wrong in the first place.

I think the point is that in this notation the value that is substituted for % is far removed from its point of insertion, which requires you to read back and forth when understanding the code. Perhaps read the linked PEP article; I believe Stefaan was more or less paraphrasing that.

-Brian

On Fri, Dec 22, 2017 at 11:34 AM, <ambrose.li@xxxxxxxxx> wrote:
2017-12-22 9:47 GMT-05:00  <stefaan.himpe@xxxxxxxxx>:
> More recently python added literal string interpolation (also known as
> "f-strings") that I find more convenient to use because they don't require
> me to rewrite sentences "in % word order" % ["garbled"]
> https://www.python.org/dev/peps/pep-0498/

Sorry my python is so rusty, but why would % require you to rewrite
sentences in % order? If we need to do this in python they're doing %
wrong in the first place. I've never heard of this problem either in C
or in Perl (I'm aware that $ is technically not standardize, but no
modern open source program would work without $).

String interpolation (and raw string literals) came from Perl. When I
write Perl I actually go back to % because interpolated strings can
quickly become completely unreadable.

--
Ambrose Li // http://o.gniw.ca / http://gniw.ca
If you saw this on CE-L: You do not need my permission to quote
me, only proper attribution. Always cite your sources, even if
you have to anonymize and/or cite it as "personal communication".