New statement proposal for Python
Delaney, Timothy
tdelaney at avaya.com
Sun Jun 17 22:51:04 EDT 2001
[Lots of stuff snipped from people, primarily Alex and Dave]
I'm having a lot of trouble with this proposal. I believe I am against parts
of it in principal, but am heavily in favour of the basic idea behind it
(which I have expressed before).
1. I am heavily against the use of the "alias:" syntax. However, I will use
the "alias" terminology here.
2. I am unclear about what is actually being proposed. I have seen
conflicting statements by Dave, so wish to clear it up. Please choose one of
the following.
a. An alias would perform a textual substitution before the code is compiled
(as in a C #define).
b. An alias would be evaluated where it is assigned/created, and thereafter
its value would be constant.
c. An alias would be evaluated using the local namespace each time it is
encountered, and the result used in the expression.
In my mind, (b) is a constant, and (c) is an alias. (a) should not even be
considered.
The root of the problem which I believe people are trying to solve is making
things provably constant. There are two things involved here:
1. Preventing a reference from being rebound.
2. Preventing a mutable instance from being modified.
Both of these are almost completely doable now.
1. Preventing a reference from being rebound:
As Alex has posted, and I agree, replacing a module with a restricted
__setattr__() does most of what we wish here. A perhaps better option though
would be to have a builtin object called "const". This has a few advantages,
and 1 disadvantage:
a. There is no need to "import const" (advantage).
b. It does not interfere with a module named "const" created by someone else
- local namespace takes precedence over builtin (advantage).
c. The __builtin__.__dict['const'] object could be replaced by something
else (normally a disadvantage). This could be changed by making
__builtin__.__dict__ not actually be a dictionary, but a class instance
which disallows rebinding certain references (such as "const").
2. Preventing a mutable instance from being modified
This could be done for class instances by an immutable() builtin function.
This function would change the __setattr__() function of the instance to one
which raises an Exception, and nothing else.
a. This would only work for class instances - not classes or types. With the
type/class difference going away, it comes down to only class objects having
problems with this. Until this difference goes away, lists could be
converted to tuples by immutable(), but dictionaries remain a problem.
b. Someone could get around the immutable() function by replacing the
__setattr__() method after it is called.
Tim Delaney
More information about the Python-list
mailing list