On 06/03/2011 02:37, John Nagle wrote:
On 3/2/2011 9:27 PM, Steven D'Aprano wrote:
On Wed, 02 Mar 2011 19:45:16 -0800, Yingjie Lan wrote:

Hi everyone,

Variables in Python are resolved dynamically at runtime, which comes at
a performance cost. However, a lot of times we don't need that feature.
Variables can be determined at compile time, which should boost up
speed.
[...]

This is a very promising approach taken by a number of projects.

It's worth having some syntax for constants. I'd suggest
using "let":

let PI = 3.1415926535897932384626433832795028841971693993751

I'd propose the following semantics:

1. "let" creates an object whose binding is unchangeable. This
is effectively a constant, provided that the value is immutable.
A compiler may treat such variables as constants for optimization
purposes.

2. Assignment to a a variable created with "let" produces an error
at compile time or run time.

3. Names bound with "let" have the same scope as any other name
created in the same context. Function-local "let" variables
are permitted.

4. It is an error to use "let" on a name explicitly made "global",
because that would allow access to the variable before it was
initialized.

This is close to the semantics of "const" in C/C++, except that
there's no notion of a const parameter.

"let" allows the usual optimizations - constant folding, hoisting
out of loops, compile time arithmetic, unboxing, etc. Ordinarily,
Python compilers have to assume that any variable can be changed
at any time from another thread, requiring worst-case code for
everything.

Having a fixed binding could be useful elsewhere, for example, with
function definitions:

    const PI = 3.1415926535897932384626433832795028841971693993751

    const def squared(x):
        return x * x

or:

    fixed PI = 3.1415926535897932384626433832795028841971693993751

    fixed def squared(x):
        return x * x
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to