Will python never intend to support private, protected and public?

Antoon Pardon apardon at forel.vub.ac.be
Mon Oct 3 05:14:34 EDT 2005


Op 2005-09-30, Rocco Moretti schreef <roccomoretti at hotpop.com>:
> Antoon Pardon wrote:
>
>>>What if the class author removes a non-private variable or changes a 
>>>method's documented parameters in the next version of the class, because 
>>>he think it'll work better, or just because he can?
>> 
>> Changing an interface is different from changing the implementation.
> > A (documented) interface is like a contract. The implementation is
> > just one way to follow that contract.
>
> Agreed. However, there is also a difference between an interface and 
> "non-private variables."
>
> E.g. you have a library, and playing with the code, you notice that by 
> passing an empty string as a filename, you get the last file accessed. 
> Cool. You write your program using this feature. Problem is, it's a 
> quirk of the implementation, and in the next version, the library author 
> fixes this "bug". Preventing access to private variables wouldn't help - 
> the only thing you touched was the public parameter to a public function.

But this behaviour of a particular client can't break your library.

> Of course, you could have avoided this by only using the documented 
> interface, but if we go that route, you wouldn't have to worry about 
> people accessing private variables, as they wouldn't be documented.

I'm not so much worried about client software that breaks, as I'm
worried about server code that breaks because a client fiddled
with the private stuff.

> There is little in the way of technical problems that are solved by 
> language level enforcement of private variables. The issues in question 
> are mostly social ones, and if you're not reading and following the 
> documented interface, stopping private variable access is not going to 
> prevent most of your problems.

It will prevent problems for others. If you are in a project with
multiple authors, your usage of private variables can break code
that other people rely on.

I you just use undocumented features, you will mostly only create
problems in your own code.

>>> People who think that forbidding access to private variables/methods 
>>> will save themselves from upgrade woes are deluding themselves.
> >
>> It helps, just as locks wont save you from burglars if they really
>> want to rob you, but the locks do help.
>
> Right, but like doors that automatically lock when they close, items 
> which are there to protect you can be a nusaince, especially when you've 
> left your keys on the dining room table.

It is always a trade off. Sometimes you settle for more protection and
less comfort other times you do the opposite. If private variables
would be possible, nobody would force you to use them if you didn't
want to.

-- 
Antoon Pardon



More information about the Python-list mailing list