[Python-ideas] Custom string prefixes

Andrew Barnert abarnert at yahoo.com
Thu May 30 07:41:39 CEST 2013


On May 29, 2013, at 22:21, Eric Snow <ericsnowcurrently at gmail.com> wrote:

> Now, give a twist to this idea of some stable `literal -> contstant`
> operation. Consider a new syntax or built-in function for indicating
> that, at compile-time, an expression should be considered equivalent
> to the literal to which it evaluates.  Then the compiler would be free
> to substitute the literal for the expression and store the literal in
> the bytecode/constants/pyc file.  From then on that expression would
> not be evaluated at run-time anymore.  Of course, the expression would
> have to be entirely literal-based and evaluate to a literal.

So you're suggesting that instead of C++11-style string prefixes, we should have C++11-style constexpr.

I realize your ultimate conclusion was that we probably don't need _either_ feature. But still, it's amazing how C++11-ish this discussion is getting. Which may be a good hint that (as you suggest) this feature isn't a good fit for Python. 

Unless someone has a way of doing it through compile-time templates, of course. :)

> The catch is that the compiler would have to evaluate the expression,
> which would probably add disproportionate complexity to the compiler.

I don't think it's that bad; the compiler just has to make a call to a PyEval* function.

Also, given that your proposal is that it be explicitly an optional optimization that the compiler is free to ignore means there's an even more trivial implementation...


More information about the Python-ideas mailing list