<div dir="ltr">Y'know, I just read a few more posts over on python-ideas that I had missed somehow. I saw Guido's point about `**locals()` being too specialized and magical for beginners, which I agree with. And it's the other aspect of "magic" that makes me not like f-strings. The idea of *implicitly* getting values from the local scope (or really, the global_local_builtin scope) makes me worry about readers of code very easily missing what's really going on within an f-string.<div><br></div><div>I don't actually care about the code injection issues and that sort of thing. I mean, OK I care a little bit, but my actual concern is purely explicitness and readability.</div><div><br></div><div>Which brought to mind a certain thought. While I don't like:</div><div><br></div><div> f'My name is {name}, my age next year is {age+1}'</div><div><br></div><div>I wouldn't have any similar objection to:</div><div><br></div><div> 'My name is {name}, my age next year is {age+1}'.scope_format()</div><div> <br><div>Or</div><div><br></div><div> scope_format('My name is {name}, my age next year is {age+1}')</div><div><br></div><div>I realize that these could be completely semantically equivalent... but the function or method call LOOKS LIKE a runtime operation, while a one letter prefix just doesn't look like that (especially to beginners whom I might teach).</div><div><br></div><div>The name 'scope_format' is ugly, and something shorter would be nicer, but I think this conveys my idea.</div></div><div><br></div><div>Yours, David...</div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Aug 9, 2015 at 6:14 PM, David Mertz <span dir="ltr"><<a href="mailto:mertz@gnosis.cx" target="_blank">mertz@gnosis.cx</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span class="">On Sun, Aug 9, 2015 at 11:22 AM, Eric V. Smith <span dir="ltr"><<a href="mailto:eric@trueblade.com" target="_blank">eric@trueblade.com</a>></span> wrote:<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><span style="color:rgb(34,34,34)">I think it has to do with the nature of the programs that people write.</span><br></div></div>
I write software for internal use in a large company. In the last 13<br>
years there, I've written literally hundreds of individual programs,<br>
large and small. I just checked: literally 100% of my calls to<br>
%-formatting (older code) or str.format (in newer code) could be<br>
replaced with f-strings. And I think every such use would be an improvement.<br></blockquote><div><br></div></span><div>I'm sure that pretty darn close to 100% of all the uses of %-formatting and str.format I've written in the last 13 years COULD be replaced by the proposed f-strings (I suppose about 16 years for me, actually). But I think that every single such replacement would make the programs worse. I'm not sure if it helps to mention that I *did* actually "write the book" on _Text Processing in Python_ :-).</div><div><br></div><div>The proposal just continues to seem far too magical to me. In the training I now do for Continuum Analytics (I'm in charge of the training program with one other person), I specifically have a (very) little bit of the lessons where I mention something like:</div><div><br></div><div> print("{foo} is {bar}".format(**locals()))</div><div><br></div><div>But I give that entirely as a negative example of abusing code and introducing fragility. f-strings are really the same thing, only even more error-prone and easier to get wrong. Relying on implicit context of the runtime state of variables that are merely in scope feels very break-y to me still. If I had to teach f-strings in the future, I'd teach it as a Python wart.</div><div><br></div><div>That said, there *is* one small corner where I believe f-strings add something helpful to the language. There is no really concise way to spell:</div><div><br></div><div> collections.ChainMap(locals(), globals(), __builtins__.__dict__).</div><div><br></div><div>If we could spell that as, say `lgb()`, that would let str.format() or %-formatting pick up the full "what's in scope". To my mind, that's the only good thing about the f-string idea.</div></div><div class="gmail_extra"><br></div>Yours, David...<span class="HOEnZb"><font color="#888888"><br clear="all"><div><br></div>-- <br><div>Keeping medicines from the bloodstreams of the sick; food <br>from the bellies of the hungry; books from the hands of the <br>uneducated; technology from the underdeveloped; and putting <br>advocates of freedom in prisons. Intellectual property is<br>to the 21st century what the slave trade was to the 16th.<br></div>
</font></span></div></div>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature">Keeping medicines from the bloodstreams of the sick; food <br>from the bellies of the hungry; books from the hands of the <br>uneducated; technology from the underdeveloped; and putting <br>advocates of freedom in prisons. Intellectual property is<br>to the 21st century what the slave trade was to the 16th.<br></div>
</div></div>