[Python-ideas] 'const' and 'require' statements

Chris Angelico rosuav at gmail.com
Fri Jan 18 07:31:52 CET 2013


On Fri, Jan 18, 2013 at 3:52 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> Another question: what happens if the constant expression can't be
> evaluated until runtime?
>
> x = random.random()
> const k = x + 1
>
> y = k - 1
>
> What value should the compiler substitute for y?

That should be disallowed. In the declaration of a constant, you have
to use only what can be handled by the constants evaluator. As a rule
of thumb, it'd make sense to be able to use const with anything that
could safely be evaluated by ast.literal_eval.

As to the issues of rebinding, I'd just state that all uses of a
particular named constant evaluate to the same object, just as would
happen if you used any other form of name binding.

I don't have the post to hand, but wasn't there a project being
discussed recently that would do a lot of that work automatically?

ChrisA



More information about the Python-ideas mailing list