473,326 Members | 2,805 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,326 software developers and data experts.

making objects unassignable "read-only" (especially when extending)

Hi,

can I make an object read-only, so that

x = new_value

fails (and x keeps it's orginal value)?

This would be especially of interest for me for an object created by
a c extension.

--
Johannes
Jan 18 '06 #1
3 3569
On 18 Jan 2006 11:59:23 GMT, Johannes Zellner <jo******@zellner.org> wrote:
Hi,

can I make an object read-only, so that

x = new_value

fails (and x keeps it's orginal value)?


This works for me:

http://groups.google.com/group/comp....1785397358ff90

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
Jan 18 '06 #2
Johannes Zellner wrote:
Hi,

can I make an object read-only, so that

x = new_value

fails (and x keeps it's orginal value)?


Simon gave you a way of doing it when x is an attribute access (e.g.
p.x). I am unaware of a way of doing it when x is a straight global or
local. Unlike other languages like C, the object pointed to by a
variable is in no way involved in the assignment process. Variables are
just labels (sticky notes) attached to objects. Assignment is moving of
the label - this only involves the interpreter, and the object currently
labeled by the variable is not consulted. If you haven't seen it before,
http://starship.python.net/crew/mwh/...jectthink.html is a good read.

Note that Simon's trick works not because it changes the object pointed
to by the variable, but because it changes the properties of the
namespace where that variable lives (the p in the p.x). To do so for a
local or a global variable would require changing the local & global
namespaces - i.e. rewriting the interpreter.
Jan 18 '06 #3
Johannes Zellner wrote:
can I make an object read-only, so that

x = new_value

fails (and x keeps it's orginal value)?


the original x does contain the original value; assignment only changes
the binding in the target namespace. you cannot override this.

you can control attribute assignment, though:

x.y = new_value

(via __setattr__ methods/slots on the x object)

</F>

Jan 18 '06 #4

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

Similar topics

1
by: ParkPD | last post by:
Hi, I have been made a signed-applet that handling xml data from web-server. When I execute this applet with appletviewer, it works well. And it is OK with Internet Explorer. But when I opened...
4
by: Askari | last post by:
Yesterday, ALL code in python work and nothing when I close(finish) a code. Today, when I close, some raise this windows error : Instruction at "0x00FC3D70" use memory address "0x00000000". Can't...
10
by: ZafT | last post by:
Thanks in advance for any tips that might get me going in the right direction. I am working on a simple exercise for school that is supposed to use read to read a file (about 10 MB). I am...
0
by: antsays | last post by:
I am trying to serialize a collection to disk using the code provided. This works just fine but when I try do copy and past the xml file to another location or sometimes even just click on the...
2
by: Ryan Linneman via .NET 247 | last post by:
Any help appreciated... I am using a third party DLL file to access an HMI using ethernet. The function is declared in a VB module as: 'Read internal word address Public Declare Function...
4
by: MarkusR | last post by:
If I run the stored proc in the Query Analyzer this works and I get the expected result set back. However when I run it in my application I get a results set of one row but when I try to access the...
3
by: divsTiw | last post by:
I want to populate combo box with data from OracleDataReader , but "Invalid attempt to read when no data is present." is thrown. there are two rows returned , then too why such error. plzzz...
6
by: Lighter | last post by:
How to read "The lvalue-to-rvalue, array-to-pointer, and function-to- pointer standard conversionsare not applied to the left expressions"? In 5.18 Comma operator of the C++ standard, there is a...
3
by: mohammadazim | last post by:
Hi guys I wrote a socket program in c++ and used blocking system call read () for receiving data on socket. But it gives following error while compilation g++ -Wall -g -I`pwd`/../include...
1
by: kamcap | last post by:
I have referenced a COM dll (unmanagged code) in a C# console application and this DLL is referenced in C# program using Interop facility in .NET. This dll actually a dataset/table which is written...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.