[Python-ideas] Custom string prefixes
Chris Angelico
rosuav at gmail.com
Mon May 27 15:18:06 CEST 2013
On Mon, May 27, 2013 at 8:41 PM, Göktuğ Kayaalp
<goktug.kayaalp at gmail.com> wrote:
> - I know that there is a library called `decimal`, which provides
> facilities for finer floating point arithmetic. A `Decimal`
> class is used to express these numbers and operations, resulting in
>
> >>> decimal.Decimal ("1.6e-9") * decimal.Decimal ("1.0e9")
>
> which is a little bit messy. This can easily be cured by
>
> >>> from decimal import Decimal as D
> >>> D ("1.6e-9") * D ("1.0e9")
>
> but I'd enounce that the following is more concise and readable:
>
> >>> D"1.6e-9" * D"1.0e9"
>
> with removed parens.
Your wording is a little confusing, as you're no longer talking about
a string literal; but ISTM you're talking about something very similar
to the C++11 standard's new feature of user-defined literals:
http://en.wikipedia.org/wiki/C%2B%2B11#User-defined_literals
This may be a little too complex for what you're proposing, but it is
along the same lines. I suspect a generic system for allowing Decimal
and other literals would be welcomed here.
> Even though `str.format (*, **)` is cool, I think using an
> 'interpolated string' prefix can help clean up stuff a little bit:
>
> # ...
> def build ():
> t0 = _build.CompilationTask ([...], I"{OUTDIR}/{progn}", ...)
>
> def clean ():
> shell.sh (I"rm -fr {OUTDIR} *.o .pokedb")
Please no. It's far too easy to make extremely messy code this way. If
you want it, spell it like this:
shell.sh ("rm -fr {OUTDIR} *.o .pokedb".format(**globals()))
(or locals() perhaps) so it's sufficiently obvious that you're just
casually handing all your names to the format function. It's like
avoiding Python 2's input() in favour of explicitly spelling it out as
eval(raw_input()) - put the eval call right there where it can be
seen. The system of interpolations as found in other languages (I'm
most familiar with the PHP one as I have to work with it on a daily
basis) is inevitably a target for more and more complexity and then
edge cases; being explicit is the Python way, so unless there's a
really good reason to make all your global names easily available, I
would be strongly inclined to not.
> I'm looking forward to your criticisms and advices. I've searched this
> online and asked in the chatroom (#python) and I'm nearly sure that I'm
> not asking for a feature that is already present. Being a beginner, I
> can say that I'm kind of nervous to post here, where really experienced
> people discuss the features of an internationally-adopted language.
I'd recommend python-list at python.org or comp.lang.python rather than
#python; you get much better responses when there's no requirement for
people to be online simultaneously. But in this case you're right,
there's no feature quite as you describe.
In fact, I'd recommend you join python-list regardless, if only
because we have awesome fun there :) You sound like you'd be the
perfect sort to join in.
ChrisA
More information about the Python-ideas
mailing list