Gregor Lingl wrote: > > Peter Hansen schrieb: > > > What exactly do you need this for? If you describe the purpose > > you have in mind for it we can provide the best approach, or > > tell you that we don't think you should bother. ;-) > > > > -Peter > > I'm writing a module for teaching young students. It contains > e. g. a function width, which assigns a value to some (hidden) > variable: > > >>> width(5) > > Now my experience is, that from time to time some of my > students write (erroneously) > > >>> width = 5 > > 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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4