How do you create constants?

Huaiyu Zhu hzhu at users.sourceforge.net
Mon Oct 30 14:57:52 EST 2000


On 28 Oct 2000 20:43:54 GMT, Quinn Dunkan <quinn at bolivar.ugcs.caltech.edu>
wrote: 
>
>One real use I can think of for enforced immutable values is efficiency (e.g.,
>sather's immutable classes which enable some optimization), and even though
>python is not an efficiency-minded language, it still provides tuples (and
>immutable strings which probably also has something to do with the popularity
>of keying dicts with them).

Taking this idea to a fuller generality: -

Python objects do have the property of constancy: number, string and tuple
are immutable, list, dictionary, function, class and module are mutable.
Python names are mutable - any name can refer to any object whatsoever.

So a hyperthetical Python with the const keyword might look like this:

x = const "a string" # as current string
x = "a string"       # a true class object that's mutable
x = [1, 2, 3]        # as current list
x = const [1, 2, 3]  # as current tuple
const x = [1, 2, 3]  # x is fixed to a list whose contents can change
const x = const [1, 2, 3] # x is fixed to a const list (tuple)
x = const {1:2, 3:4} # a dict whose contents cannot change

Is this a good thing?
- It gives programmers more expresiveness when needed.
- It does not force a change in programming style if not wanted.
- It may allow substantial efficiency gains.
- It allows more consistency for things like augmented assignment.
- It may be combined with safe exec module to build other security modules.

Any potential problems?
What's the implementational cost? 

Huaiyu



More information about the Python-list mailing list