[Python-ideas] 'const' statement

Ben Finney ben+python at benfinney.id.au
Fri Jan 18 04:37:21 CET 2013
"Harding, James"
<james.d.harding at siemens.com> writes:

> The first idea is for a 'const' statement for declaring constant
> names.

Do you have some concrete Python code which would clearly be improved by
this proposal?

> Its syntax would be:
>
>     'const' identifier '=' expression
>
> The expression would be restricted to result in an immutable object
> such as 17, "green", or (1,2,3). The compiler would effectively replace
> any use of the identifier with this expression when seen. Some examples
> of constants might include:
>
>     const ST_MODE = 0
>     const FontName = "Ariel"
>     const Monday = 1
>     const Tuesday = Monday + 1     # may use previously defined const
>     in expression. Compiler will fold constants (hopefully)

So, the compiler will “replace any use of the identifier with” the
constant value.

    const ST_MODE = 0
    const ST_FILENAME = "foo"
    const ST_RECURSIVE = True

    name_prefix = "ST_"
    foo = globals().get(name_prefix + "MODE")
    bar = globals().get(name_prefix + "FILENAME")
    baz = globals().get(name_prefix + "RECURSIVE")

What do you expect the compiler to do in the above code?

-- 
 \     “Airports are ugly. Some are very ugly. Some attain a degree of |
  `\        ugliness that can only be the result of a special effort.” |
_o__)       —Douglas Adams, _The Long Dark Tea-Time of the Soul_, 1988 |
Ben Finney




More information about the Python-ideas mailing list