New statement proposal for Python

Alex Martelli aleaxit at yahoo.com
Sun Jun 17 16:04:27 EDT 2001


"phil hunt" <philh at comuno.freeserve.co.uk> wrote in message
news:slrn9iplq1.64s.philh at comuno.freeserve.co.uk...
    ...
> >I think this is a slight overbid.  It is perfectly possible in Python to
> >"block rebinding" of an object's attribute (__setattr__ easily allows
    ...
> 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,

If user objects were allowed as a module's __dict__, and/or if
built-in dictionaries allowed selective locking of entries, you could
do that (if by "current environment" you mean the GLOBAL one --
a function cannot add entries to its caller' LOCAL namespace, in
general... only a statement can do that).  In Python 2.1 at least,
you can do that in a dedicated "module" (let's call it 'const' for
definiteness), actually implemented as an instance -- I showed
similar ideas repeatedly in this thread.  Things could be set up
as to allow a more natural
    const.PI = 3.14159
to be completely equivalent to your hypothetical CONST function
(if your CONST could bind and lock a variable in its caller's global
namespace, so could const.__setitem__, of course).

> >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?

In Python 2.1 at least (and I doubt this will change in 2.2) it is
not yet possible to use a user-object as a module's dictionary,
nor do builtin dictionaries support selective locking.  I suspect
the former stricture WILL eventually fall (as part of the long slow
march to overcome the type/class chasm), but I would not venture
a guess/prediction about the latter.


Alex






More information about the Python-list mailing list