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

Readonly attributes ...

Hi all python users,

A question about readonly attributes once again, but
even reading the threads about "properties" and "descriptors",
I can't find exactly what I want.

Let a developper to write the class

class A(object):
def __init__(self):
self.x = 1
self.setX(2)
return
def getX(self):
return self.__x
def setX(self, value):
self.__x = value
return
x = property(getX, setX)
pass

Of course, the developper can set the attribute 'x'
in the class ...

Now, the user part :

a = A()
print a.x
a.x = 1 # I want this operation to raise
print a.x
a.setX(23) # I want this operation to raise too !!
print a.getX()

I can't find a way to do that ... but may be I've
missed something !

Cheers,

K.
Jul 18 '05 #1
1 1400
It is cumbersome but you can do it as follows:

class A:
_ROattributes=['x', '_A__x']

def __init__(self):
#self.x = 1
self.__dict__['x']=1 # Sets x without calling __setattr__
self.__setX(2)
return

def getX(self):
return self.__x
#
# Use name mangling to hide setX (by renaming to __setX)
# to outside execution. Isn't perfect, but follows convention.
#
def __setX(self, value):
self.__dict__['_A__x'] = value
return

def __setattr__(self, key, value):
#print "in __setattr__, key=",key
if key in self._ROattributes:
print "Read Only attribute %s, not set" % key

else:
self.__dict__[key]=value

#x = property(getX, setX)
#pass

a=A()
print "a.x=", a.x
a.x=1
a._A__setX(23) # You can call it if you REALLY try
print a.getX()


-Larry Bates

"kobayashi" <ko*******@netcourrier.com> wrote in message
news:fe**************************@posting.google.c om...
Hi all python users,

A question about readonly attributes once again, but
even reading the threads about "properties" and "descriptors",
I can't find exactly what I want.

Let a developper to write the class

class A(object):
def __init__(self):
self.x = 1
self.setX(2)
return
def getX(self):
return self.__x
def setX(self, value):
self.__x = value
return
x = property(getX, setX)
pass

Of course, the developper can set the attribute 'x'
in the class ...

Now, the user part :

a = A()
print a.x
a.x = 1 # I want this operation to raise
print a.x
a.setX(23) # I want this operation to raise too !!
print a.getX()

I can't find a way to do that ... but may be I've
missed something !

Cheers,

K.

Jul 18 '05 #2

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

Similar topics

1
by: James Lawyer | last post by:
I am trying to use SearchRequest.Attributes property in System.DirectoryServices.Protocols to specify the attributes returned by an LDAP search. However, I cannot assign a value to this readonly...
3
by: Michael SL | last post by:
I have a text area in which I have a client side javascript to process a "onclick". Because it is client side, I used a HtmlTextArea <TextArea id="Summary" onmouseup="SumMouseUp()" style="WIDTH:...
2
by: Raed Sawalha | last post by:
I'm reading XML files using XmlDocument, in some cases i have XML marked with readonly, so when tried to write to them an Access Denied exception is generated, is there a way to change the XML...
0
by: Brian Young | last post by:
Hi all. I'm using the Property Grid control in a control to manage a windows service we have developed here. The windows service runs a set of other jobs that need to be managed. The control...
2
by: Daren Hawes | last post by:
Hi I need to add an attribute to a Textbox to make it read only. To add a CSS I use DeptDate.Attributes("Class") = "textInput" That adds 'Class="textinput"', but the readonly is like..
2
by: SivaprakashShanmugam | last post by:
Hi Can you please explain me how to make a text file as a Read Only which is already existing in local disk in C#. Thanks Siva.
9
by: ShaneFowlkes | last post by:
Hey guys... I have a form that asks for several dates. I tried using asp calendar controls to set values of the date textboxes which were readonly. I found this annoying since each time I...
1
by: Thierry | last post by:
Hi, We are currently upgrading our .Net web apps from framework 1.1 to 2.0 Everything is going quite smoothly except a bug with textbox value not rendered when having propery readonly=true on the...
3
by: Hamed | last post by:
Hello I have a DataGrid object in my ASP.NET page that has the following template column. When I put the "readonly" attribute in the INPUT tag, it generates readonly="". <asp:TemplateColumn...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.