how to write-protect names

G

Gregor Lingl

Hi,

is it possible to "write-protect" a definite
set of names of a module, especially of the
__main__ module?
If so, how?

Regards, Gregor
 
P

Peter Hansen

Gregor said:
is it possible to "write-protect" a definite
set of names of a module, especially of the
__main__ module?
If so, how?

Almost anything is possible at some level in Python, although,
likewise, almost anything can be broken at another level.

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
 
G

Gregor Lingl

Peter said:
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:

Now my experience is, that from time to time some of my
students write (erroneously)

which renders the function width unaccessible for future
use. (Moreover the next

will result in a rather strange error-message, like
int-object is not callable ... (my students won't
underswtand it).

Wouldn't it be useful if the name width were write-protected

Regards,
Gregor
 
P

Peter Hansen

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
 
P

python

Can you post a trivial example of how to use __setattr__() and how to
set the namespace in the interpreter?

Thanks.
 
A

anton muhin

python said:
Can you post a trivial example of how to use __setattr__() and how to
set the namespace in the interpreter?

Thanks.

Really basic one:

class Namespace(object):
def __setattr__(self, name, value):
if name in self.__dict__:
raise "oops"
object.__setattr__(self, name, value)

util = Namespace()

util.x = 1
print util.x

util.x = 2
 
L

Lee Harr

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. (Moreover the next


will result in a rather strange error-message, like
int-object is not callable ... (my students won't
underswtand it).

Wouldn't it be useful if the name width were write-protected


Maybe you could do something with the code module
(InteractiveConsole, compile_command, etc)

You get to set up a hook that is called each time a new
input line comes in... so you could watch for your special
names and just print a helpful reminder when someone tries
to overwrite one.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top