having both dynamic and static variables

John Nagle nagle at animats.com
Sat Mar 5 21:37:48 EST 2011


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	



More information about the Python-list mailing list