On Sat, 2011-03-05 at 18:37 -0800, 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. > > John Nagle I'm against constants, for the purpose of "programmers should be smart enough to not set a variable to another value that should be static", but if Python were to have constants I think it would be better to use something more descriptive than 'let'. Also, because the defined constant is static, I think it would be better to use 'is' instead of '='. Example:
constant x is 5 -- http://mail.python.org/mailman/listinfo/python-list