473,387 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

how to write-protect names

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

Jul 18 '05 #1
6 1639
Gregor Lingl wrote:

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
Jul 18 '05 #2
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. (Moreover the next
width(10)


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

Jul 18 '05 #3
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
Jul 18 '05 #4
Can you post a trivial example of how to use __setattr__() and how to
set the namespace in the interpreter?

Thanks.
On Wed, 17 Sep 2003 17:42:19 -0400
Peter Hansen <pe***@engcorp.com> wrote:
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
--
http://mail.python.org/mailman/listinfo/python-list


Jul 18 '05 #5
python wrote:
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

Jul 18 '05 #6
In article <3F************@aon.at>, 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. (Moreover the next
width(10)


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.

Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Greg Hurlman | last post by:
I've got what I'm sure is a very simple problem. In an ASP page, I am trying to write out 4 fields from a recordset in succession: Response.Write rs("LastName") Response.Write rs("Suffix")...
1
by: techy techno | last post by:
Hii Just wanted to know how can I decorate my texboxes and Listmenu which is called from a JS file using the following code below: document.write("<SELECT NAME='cur2' ONCHANGE='cconv1();'>");...
2
by: Brett Baisley | last post by:
Hello I have a block of html code that I want to run by calling a javascript function to print it. Its basically a table with menu items in it that is the same for many pages, and instead of...
0
by: hari krishna | last post by:
hi all, My requirement is to generate xl reports throu Asp.Net without installing xl on web server computer. i am using Response object and wrtifile method as below. i dont know whether it is...
8
by: Ben | last post by:
Hi all, Just wondering how to write (using document.write) to a table cell. I have table with 3 rows and 3 colums. I want to write from within the Javascript to say third column of a first row....
4
by: Prowler | last post by:
In the application we are currently building, we need to write positioning code on-the-fly, based upon the screen offset of the element in the AS/400 application which drives the Web app. The 400,...
11
by: Vmusic | last post by:
Hi, I am trying to write out an array of string variables to Notepad. I can't get SendKeys to accept the string variable only literal quoted strings. I DO NOT want the hassle of writing to a...
4
by: cbtechlists | last post by:
I have an ASP app that we've moved from a Windows 2000 to a Windows 2003 server (sql server 2000 to sql server 2005). The job runs fine on the old servers. Part of the app takes a recordset and...
0
by: kuguy | last post by:
Hi all, I'm new to the forums, so I hope this isn't in the wrong place... I have that "Software caused connection abort: socket write error" exception error that i've never meet before. ...
8
by: Mateusz Viste | last post by:
Hi, I am trying make some multimedia files playable from my website. So far, I am able to generate dynamically a new page containing the right <embed> section. However, when I load my script, it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.