<div dir="ltr">With <a href="https://github.com/lihaoyi/macropy#string-interpolation">macros</a>, you can string interpolation kinda like that:<div><pre style="margin-top:15px;margin-bottom:15px;padding:6px 10px;border:1px solid rgb(221,221,221);font-size:13px;font-family:Consolas,'Liberation Mono',Courier,monospace;background-color:rgb(248,248,248);line-height:19px;overflow:auto;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;color:rgb(51,51,51)">
<span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">>>></span> <span class="" style="margin:0px;padding:0px;border:0px">a</span><span class="" style="margin:0px;padding:0px;border:0px">,</span> <span class="" style="margin:0px;padding:0px;border:0px">b</span> <span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">=</span> <span class="" style="margin:0px;padding:0px;border:0px;color:rgb(0,153,153)">1</span><span class="" style="margin:0px;padding:0px;border:0px">,</span> <span class="" style="margin:0px;padding:0px;border:0px;color:rgb(0,153,153)">2</span>
<span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">>>></span> <span class="" style="margin:0px;padding:0px;border:0px">s</span><span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">%</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(221,17,68)">"%{a} apple and %{b} bananas"</span>
<span class="" style="margin:0px;padding:0px;border:0px;color:rgb(221,17,68)">'1 apple and 2 bananas'</span></pre></div><div style>But enough with the self promotion =D. </div><div style><br></div><div style>> <span style="font-family:arial,sans-serif;font-size:13px">What do you think</span></div>
> <br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">> s"ham"</span><br style="font-family:arial,sans-serif;font-size:13px">> <br style="font-family:arial,sans-serif;font-size:13px">
<span style="font-family:arial,sans-serif;font-size:13px">> will mean to the reader? I think that it is better to encourage people to write meaningful names:</span><br style="font-family:arial,sans-serif;font-size:13px">
> <br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">> make_sandwich("ham")</span><br style="font-family:arial,sans-serif;font-size:13px"><div style>
<br></div><div style>I disagree that custom string prefixes make code harder to read. Saying s"ham" costs minutes of confusion over s("ham") is based purely on unfamiliarity, which can be removed over time. Why u"text" rather than make_unicode_string("text") if the latter is more meaningful? </div>
<div style><br></div><div style>This may be entirely mistaken, but </div><div style><ul style><li style>If the parsing of string literals could be shifted from the lexer/parser into functions being called at import time to preprocess the interned literals<br>
</li><li style>If hooking into this preprocessing could be made easy (via a simple desugaring, import-hook style, or otherwise). Naively making b"12" call the function "b" is probably a bad idea, since people using variables called "b" all the time.</li>
<li style>If existing prefixes would continue with identical semantics and minimal performance changes (e.g. from a change from lex-time handling to user-land preprocessing)</li></ul><div style>Then this could be pretty nice.</div>
<div style><br></div><div style>> <span style="font-family:arial,sans-serif;font-size:13px">I think string prefixes aren't something that we want more of, this is already complicated enough:</span></div><div style="font-family:arial,sans-serif;font-size:13px">
> "b" | "B" | "br" | "Br" | "bR" | "BR" | "rb" | "rB" | "Rb" | "RB"<br></div><div style="font-family:arial,sans-serif;font-size:13px">
> "r" | "u" | "R" | "U"<br></div><div style="font-family:arial,sans-serif;font-size:13px">></div><div style="font-family:arial,sans-serif;font-size:13px">> We should be more creative on how to get rid of them.</div>
<span class="" style="font-family:arial,sans-serif;font-size:13px"><font color="#888888"></font></span><div style><br></div><div style>If-if-if all that works out, you would be able to <i>completely remove </i>the<i> (</i><span style="font-family:arial,sans-serif;font-size:13px">"b" | "B" | "br" | "Br" | "bR" | "BR" | "rb" | "rB" | "Rb" | "RB" | </span><span style="font-family:arial,sans-serif;font-size:13px">"r" | "u" | "R" | "U") from the grammar specification! Not add more to it, remove it! Shifting the specification of all the different string prefixes into a user-land library. I'd say that's a pretty creative way of getting rid of that nasty blob of grammar =D.</span></div>
<div style><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div style><span style="font-family:arial,sans-serif;font-size:13px">Now, that's a lot of "if"s, and I have no idea if any of them at all are true, but if-if-if they are all true, we could both simplify the lexer/parser, open up the prefixes for extensibility while maintaining the exact semantics for existing code.</span></div>
<div style><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div style><span style="font-family:arial,sans-serif;font-size:13px">-Haoyi</span></div></div><div style><br></div><div style><br></div>
</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, May 27, 2013 at 10:30 AM, Göktuğ Kayaalp <span dir="ltr"><<a href="mailto:goktug.kayaalp@gmail.com" target="_blank">goktug.kayaalp@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">> ... but ISTM you're talking about something very similar<br>
> to the C++11 standard's new feature of user-defined literals ...<br>
<br>
I did not know this until now, but it looks like a fine idea. I wonder<br>
how people would react to the idea of having this in Python. I can also<br>
add that this is far better than what I propose.<br>
<div class="im"><br>
> In fact, I'd recommend you join python-list regardless, if only<br>
> because we have awesome fun there :) You sound like you'd be the<br>
> perfect sort to join in.<br>
<br>
</div>I've just started to get into the community, and even though I haven't<br>
posted anything to python-list, I'm trying to read every message. Python<br>
community is really awesome!<br>
<br>
Thanks for your input!<br>
<br>
Göktuğ<br>
<div class="HOEnZb"><div class="h5"><br>
Chris Angelico <<a href="mailto:rosuav@gmail.com">rosuav@gmail.com</a>> writes:<br>
<br>
> On Mon, May 27, 2013 at 8:41 PM, Göktuğ Kayaalp<br>
> <<a href="mailto:goktug.kayaalp@gmail.com">goktug.kayaalp@gmail.com</a>> wrote:<br>
>> - I know that there is a library called `decimal`, which provides<br>
>> facilities for finer floating point arithmetic. A `Decimal`<br>
>> class is used to express these numbers and operations, resulting in<br>
>><br>
>> >>> decimal.Decimal ("1.6e-9") * decimal.Decimal ("1.0e9")<br>
>><br>
>> which is a little bit messy. This can easily be cured by<br>
>><br>
>> >>> from decimal import Decimal as D<br>
>> >>> D ("1.6e-9") * D ("1.0e9")<br>
>><br>
>> but I'd enounce that the following is more concise and readable:<br>
>><br>
>> >>> D"1.6e-9" * D"1.0e9"<br>
>><br>
>> with removed parens.<br>
><br>
> Your wording is a little confusing, as you're no longer talking about<br>
> a string literal; but ISTM you're talking about something very similar<br>
> to the C++11 standard's new feature of user-defined literals:<br>
><br>
> <a href="http://en.wikipedia.org/wiki/C%2B%2B11#User-defined_literals" target="_blank">http://en.wikipedia.org/wiki/C%2B%2B11#User-defined_literals</a><br>
><br>
> This may be a little too complex for what you're proposing, but it is<br>
> along the same lines. I suspect a generic system for allowing Decimal<br>
> and other literals would be welcomed here.<br>
><br>
>> Even though `str.format (*, **)` is cool, I think using an<br>
>> 'interpolated string' prefix can help clean up stuff a little bit:<br>
>><br>
>> # ...<br>
>> def build ():<br>
>> t0 = _build.CompilationTask ([...], I"{OUTDIR}/{progn}", ...)<br>
>><br>
>> def clean ():<br>
>> shell.sh (I"rm -fr {OUTDIR} *.o .pokedb")<br>
><br>
> Please no. It's far too easy to make extremely messy code this way. If<br>
> you want it, spell it like this:<br>
><br>
> shell.sh ("rm -fr {OUTDIR} *.o .pokedb".format(**globals()))<br>
><br>
> (or locals() perhaps) so it's sufficiently obvious that you're just<br>
> casually handing all your names to the format function. It's like<br>
> avoiding Python 2's input() in favour of explicitly spelling it out as<br>
> eval(raw_input()) - put the eval call right there where it can be<br>
> seen. The system of interpolations as found in other languages (I'm<br>
> most familiar with the PHP one as I have to work with it on a daily<br>
> basis) is inevitably a target for more and more complexity and then<br>
> edge cases; being explicit is the Python way, so unless there's a<br>
> really good reason to make all your global names easily available, I<br>
> would be strongly inclined to not.<br>
><br>
>> I'm looking forward to your criticisms and advices. I've searched this<br>
>> online and asked in the chatroom (#python) and I'm nearly sure that I'm<br>
>> not asking for a feature that is already present. Being a beginner, I<br>
>> can say that I'm kind of nervous to post here, where really experienced<br>
>> people discuss the features of an internationally-adopted language.<br>
><br>
> I'd recommend <a href="mailto:python-list@python.org">python-list@python.org</a> or comp.lang.python rather than<br>
> #python; you get much better responses when there's no requirement for<br>
> people to be online simultaneously. But in this case you're right,<br>
> there's no feature quite as you describe.<br>
><br>
> In fact, I'd recommend you join python-list regardless, if only<br>
> because we have awesome fun there :) You sound like you'd be the<br>
> perfect sort to join in.<br>
><br>
> ChrisA<br>
> _______________________________________________<br>
> Python-ideas mailing list<br>
> <a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
> <a href="http://mail.python.org/mailman/listinfo/python-ideas" target="_blank">http://mail.python.org/mailman/listinfo/python-ideas</a><br>
<br>
</div></div><span class="HOEnZb"><font color="#888888">--<br>
Göktuğ Kayaalp <<a href="mailto:goktug.kayaalp@gmail.com">goktug.kayaalp@gmail.com</a>><br>
</font></span><div class="HOEnZb"><div class="h5">_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-ideas" target="_blank">http://mail.python.org/mailman/listinfo/python-ideas</a><br>
</div></div></blockquote></div><br></div>