Gregor said:
I'm writing a module for teaching young students. It contains
e. g. a function width, which assigns a value to some (hidden)
variable:
Now my experience is, that from time to time some of my
students write (erroneously)
which renders the function width unaccessible for future
use.
Ah, good. In that case the answer is fairly simple. You
cannot "write-protect" the name in the main module, but you
could use your own namespace for the methods such as width(),
putting them in something that looks like a module but is
really an object with a __setattr__() method which prevents
"overwriting" any of the existing names. Maybe util.width()
or something like that.
The fundamental issue is really that names are merely labels
for the things themselves, and can be rebound at will. The
students aren't really overwriting anything, and the original
width() method still exists (if any other binding to it exists
anywhere), they are simply making the label "width" reference
a different object and you can't that without providing your
own interactive environment, I suspect.
(And as a result, you still can't stop the students from binding
the name "util" to something else and still screwing up the above,
but perhaps you can trust the students not to do this if you
demonstrate it and explain why it's a bad idea.)
-Peter