Why self?

Duncan Booth duncan at NOSPAMrcp.co.uk
Tue Jul 9 07:18:51 EDT 2002


Alex Martelli <aleax at aleax.it> wrote in 
news:zWvW8.46426$Jj7.1307017 at news1.tin.it:

> What I see as the fallacy of "information hiding" as an active purview
> (having pursued it diligently for lo those many years) is that it
> reflects and responds to an assumed underlying "pecking order" which,
> upon reflection, is not a best fit for most projects.  To wit: the
> designers of libraries and frameworks do an excellent job defining
> exactly what functionality the client-code programmers need; those
> client-code programmers, on the other hand, are rater clueless blokes
> who'll trample all over the carefully designed library / framework
> architecture unless strictly held in check for their own good.

I wonder if it is worth making a distinction between "information hiding" 
and "information protection".

Python supplies a limited form of hiding, which can be useful in cases 
where you want to ensure that subclasses don't accidentally reuse the same 
member names. It can be circumvented easily and fairly cleanly in those 
cases where you really need to get at a member that has been hidden. A 
greater use of Python's private members could be useful in some of the 
library, for example htmllib would be easier to use if you didn't have to 
know to avoid members such as self.literal, self.stack, self.offset which 
don't even appear in htmllib but only in its base classes.

C++ provides a limited form of protection without actually hiding anything. 
This is rarely useful for anything. It can be circumvented easily but only 
by using nasty kludges. I am sorry to admit that I have been reduced to 
using '#define private public' just before including a header file for a 
library that went too far in its use of 'private' members.

> The way some people are going ballistic about using self.whatever
> (rather than typical C++'s m_whatever, &c) can be seen as either
> tragic or ridiculous -- I'll choose the latter viewpoint, most
> particularly given the temporal coincidence with yet another
> round of the recurring saga of the joiner.join haters...

May I just apologise for triggering that last thread: I posted a followup 
to a question about join, and then went on holiday for two weeks before the 
message had left my outbox. Naturally by the time I got back the thread had 
died out but the belated escape of my followup retriggered it.

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?



More information about the Python-list mailing list