By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,994 Members | 1,166 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,994 IT Pros & Developers. It's quick & easy.

Mark attribute as read-only

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Florian Lindner wrote:
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
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.