Mark attribute as read-only

F

Florian Lindner

Hello,
how can I mark a attribute of a class as read-only (for non classmembers)?
Yes, stupid question, but the docu gave me no help.

Thanks,

Florian
 
S

Steven Bethard

Florian said:
how can I mark a attribute of a class as read-only (for non classmembers)?
Yes, stupid question, but the docu gave me no help.

Declare it as such in the documentation. ;)

If you want to provide error messages, you could alternatively define a
property:

py> class C(object):
.... def _getx(self):
.... return self._x
.... x = property(fget=_getx)
.... def __init__(self, x):
.... self._x = x
....
py> c = C(4)
py> c.x
4
py> c.x = 6
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
AttributeError: can't set attribute

STeVe
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top