(original) (raw)
On Thu, Jan 17, 2013 at 8:52 PM, Steven D'Aprano <steve@pearwood.info> wrote:
On 18/01/13 12:52, Harding, James wrote:What is the purpose of this restriction?
The first idea is for a 'const' statement for declaring constant names.
Its syntax would be:
� � �'const' identifier '=' expression
The expression would be restricted to result in an immutable object
I would like to see the ability to prevent rebinding or unbinding of
names, with no restriction on the value. If that is useful (and I think
it is), then it is useful for mutable objects as well as immutable.
�
Java has a keyword 'final' which means a variable must be bound exactly once. It is an error if it is bound more than once or not bound at all, or read before it is initialized. For example, if a class has a final non-static field foo, then the constructor \*must\* set foo. A final value may be immutable.
This catches double initialization errors among other things.
I don't know if final belongs in Python, but I'd find that more useful than const.
--- Bruce