About the use of **args

Duncan Booth duncan at NOSPAMrcp.co.uk
Wed Dec 10 09:11:07 EST 2003


Jacek Generowicz <jacek.generowicz at cern.ch> wrote in
news:tyfzne0yeqp.fsf at pcepsft001.cern.ch: 

>> Except that in Python the convention for private variables is to
>> begin them with two underscore characters, and then it is more than
>> just a convention: 
> 
> Now, I always believed that "private" in Python is spelt "_", while
> "mangle this name for me because I want to avoid name clashes in
> multiple inheritance scenarios" is spelt "__".
> 
> However, I've heard _so_ many people make the (in my opinion
> incorrect) claim that "private" is spelt "__", that I'm beginning to
> question my sanity.
> 
> Where is the Python spelling of "private" set in stone ?

Section 5.2.1 of the Python reference manual in the paragraph headed 
'Private name mangling'.

> 
> I find the suggestion that a mangled name is somehow "more private"
> than one with a single leading underscore, mildly insulting to the
> clients of your code. The implcation is that they can neither work out
> the (very complicated and cryptic[*]) mangling scheme, not use dir().

Not at all, the mangling is clearly documented. Its purpose is most 
emphatically NOT to prevent access to your clients.

> 
> If a Python programmer wants to acces a private attribute, he can do
> so, regardless of the number of leading underscores.

Good points. I suppose it all depends on your definition of 'private'. To 
me, there are two reasons why someone might want to use 'private' 
variables. One is to prevent accidental nameclashes when someone subclasses 
one of your classes, the other is because you don't trust those poxy 
programmers who are going to use your class, so you want to burn their 
fingers if they try.

The first of these, Python provides. Ok, it doesn't get it quite right (the 
name mangling should maybe include the module and package as well as the 
class), but it does a reasonable job of minimising accidental nameclashes. 
Python does not attempt to address the second of these issues, preferring 
to believe that all Python programmers are mature & responsible.

C++ fails dismally on the first count, it doesn't hide the names so adding 
a 'private member' to a base class can break the derived classes. It also 
fails on the second count, since you can easily bypass private if you wish.

To my mind, data hiding is a good reason for using private variables, but 
preventing knowledgeable users from bypassing the privacy is dubious at 
best. You are of course free to disagree.

-- 
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