Proposal for adding symbols within Python

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


On Tue, 15 Nov 2005 21:53:23 +1100, Steven D'Aprano <steve at REMOVETHIScyber.com.au> 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)})()
 >>> N.name
 42
 >>> N.name = 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 ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list