New statement proposal for Python

phil hunt philh at comuno.freeserve.co.uk
Sun Jun 17 12:13:53 EDT 2001


On Sun, 17 Jun 2001 14:14:20 +0200, Alex Martelli <aleaxit at yahoo.com> wrote:
>"Remco Gerlich" <scarblac at pino.selwerd.nl> wrote in message
>news:slrn9ip10q.88g.scarblac at pino.selwerd.nl...
>    ...
>> Adding a way to block rebinding of a name would be a horribly inconsistent
>> wart on the language.
>
>I think this is a slight overbid.  It is perfectly possible in Python to
>"block rebinding" of an object's attribute (__setattr__ easily allows
>that), after all, yet the sky hasn't fallen.  One could similarly allow
>blocking the rebinding of a container's item (you can do that for a
>user-defined container, with __setitem__, though not on dictionaries
>or lists -- tuples do "block the rebindinding" of their items).  Such
>'blocking' is occasionally useful to help avoid accidental rebinding
>of things that are not meant to be rebound.

It's just occurred to me that using this facility, you could
implements constants in python withoput having to change the
language, by having a CONST function:

CONST("PI", 3.14159)

this will put a new item in the current environment's dictionary,
PI, with the relevant value, just like PI = 3.14159 had been executed.
It will also override the __setattr__ function so that any attempt
to reassign PI will throw an exception. of course, there would
be performance penalties.

>Thus, I do not believe it would at all be "a wart of the language" if
>Python was very slightly extended to either allow a user-defined
>mapping object to be set as the __dict__ attribute of a module
>(where now a bona fide dictionary must be used instead), 

Ah -- does that mean my idea won't work?


-- 
##  Philip Hunt                   ##
##  philh at comuno.freeserve.co.uk  ##






More information about the Python-list mailing list