Proposal for adding symbols within Python

Bengt Richter bokr at
Tue Nov 15 23:21:45 CET 2005

On Tue, 15 Nov 2005 21:53:23 +1100, Steven D'Aprano <steve at> wrote:
>It isn't always appropriate or necessary to define "constants" (and I
>sometimes wish that Python would enforce assign-once names), but they can
>help avoid some silly mistakes.
(As I'm sure you know) you can have "assign-once" names
if you are willing to spell them with a dot ;-)

 >>> N = type('',(),{'name':property(lambda _:42)})()
 >>> = 43
 Traceback (most recent call last):
   File "<stdin>", line 1, in ?
 AttributeError: can't set attribute

One could also write a more efficient write-once guarantee for function scope
using a decorator that munges byte code to guarantee it.

Or one could write a custom import that guarantees it for module scope.

Or one could change the language ;-)

Bengt Richter

More information about the Python-list mailing list