[Tutor] creating read only attributes
alan.gauld at blueyonder.co.uk
Sat Nov 22 15:50:30 EST 2003
> > they probably won't do that. In general, creating
> > private variables is considered unpythonic, so it is
> > probably the same for these immutable variables.
> Isn't one of the basic tenants of OO that objects should
> be like "magic black boxes", and their inner workng
> should be invisable to outside observers?
No, its something that has been tagged on to OO in relatively
recent times. All the early OO languages allowed public access
to atttibutes but discouraged it by convention.
I think Smalltalk 78 was the first OO language to enforce
data hiding (sometimes erroneously called encapsulation) but
many OOP languages before and since have got on just
fine without it.
> The same goes for read-only attributes..... OK, so we
> could all use lots of methods, but if you ask me,
> a whole heap od methods called "set_a", "set_b",
A read only attribute is a constant. There are coding
conventions for this but actually the ability to define
a constant variabkle is something I'd like in Python
- but given Pythons way of working - all variables
are references - I just don't see how it could be
done consistently in the language. So coding conventions
are the best substitute.
More information about the Tutor